piral-oauth2 1.0.0-pre.2112 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2019 - 2021 smapiot
3
+ Copyright (c) 2019 - 2023 smapiot
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
- [![Piral Logo](https://github.com/smapiot/piral/raw/master/docs/assets/logo.png)](https://piral.io)
1
+ [![Piral Logo](https://github.com/smapiot/piral/raw/main/docs/assets/logo.png)](https://piral.io)
2
2
 
3
- # [Piral OAuth 2.0](https://piral.io) · [![GitHub License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/smapiot/piral/blob/master/LICENSE) [![npm version](https://img.shields.io/npm/v/piral-oauth2.svg?style=flat)](https://www.npmjs.com/package/piral-oauth2) [![tested with jest](https://img.shields.io/badge/tested_with-jest-99424f.svg)](https://jestjs.io) [![Gitter Chat](https://badges.gitter.im/gitterHQ/gitter.png)](https://gitter.im/piral-io/community)
3
+ # [Piral OAuth 2.0](https://piral.io) · [![GitHub License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/smapiot/piral/blob/main/LICENSE) [![npm version](https://img.shields.io/npm/v/piral-oauth2.svg?style=flat)](https://www.npmjs.com/package/piral-oauth2) [![tested with jest](https://img.shields.io/badge/tested_with-jest-99424f.svg)](https://jestjs.io) [![Gitter Chat](https://badges.gitter.im/gitterHQ/gitter.png)](https://gitter.im/piral-io/community)
4
4
 
5
5
  This is a plugin that only has a peer dependency to `piral-core`. What `piral-oauth2` brings to the table is a direct integration with OAuth 2.0 identity providers on basis of the client-oauth2 library that can be used with `piral` or `piral-core`.
6
6
 
@@ -39,7 +39,7 @@ export async function setup(piral: PiletApi) {
39
39
  }
40
40
  ```
41
41
 
42
- Note that this value may change if the Piral instance supports an "on the fly" login (i.e., a login without redirect / reloading of the page).
42
+ Note that this value may change if the Piral instance supports an "on the fly" login (i.e., a login without redirect/reloading of the page).
43
43
 
44
44
  :::
45
45
 
@@ -77,15 +77,19 @@ import { setupOAuth2Client } from 'piral-oauth2';
77
77
 
78
78
  export const client = setupOAuth2Client({ ... });
79
79
 
80
- // app.ts
80
+ // app.tsx
81
+ import * as React from 'react';
81
82
  import { createOAuth2Api } from 'piral-oauth2';
83
+ import { createInstance } from 'piral-core';
82
84
  import { client } from './oauth2';
85
+ import { render } from 'react-dom';
83
86
 
84
87
  export function render() {
85
- renderInstance({
88
+ const instance = createInstance({
86
89
  // ...
87
90
  plugins: [createOAuth2Api(client)],
88
91
  });
92
+ render(<Piral instance={instance} />, document.querySelector('#app'));
89
93
  }
90
94
 
91
95
  // index.ts
@@ -102,6 +106,28 @@ if (location.pathname !== '/auth') {
102
106
 
103
107
  This way we evaluate the current path and act accordingly. Note that the actually used path may be different for your application.
104
108
 
109
+ The chosen OAuth 2 flow makes a difference. The example above works fine with the `implicit` flow (default). If you want to use the `code` flow then the integration looks a bit different.
110
+
111
+ Example integration using the `code` flow:
112
+
113
+ ```ts
114
+ // index.ts
115
+ import { client } from './oauth2';
116
+
117
+ if (location.pathname !== '/auth') {
118
+ client.login();
119
+ } else {
120
+ client.token().then(
121
+ // all good we are logged in
122
+ () => import('./app').then(({ render }) => render()),
123
+ // something went bad, we should show some error
124
+ () => import('./error').then(({ render }) => render()),
125
+ );
126
+ }
127
+ ```
128
+
129
+ The code flow will automatically return from `/auth` to `/` once authenticated. Therefore, the application's routing does not have to consider `/auth`, which can remain a special path.
130
+
105
131
  :::
106
132
 
107
133
  ## License
package/esm/create.d.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import { PiralPlugin } from 'piral-core';
2
- import { OAuth2Client } from './setup';
3
- import { PiralOAuth2Api } from './types';
2
+ import { OAuth2Client, PiralOAuth2Api } from './types';
4
3
  /**
5
4
  * Creates new Pilet API extensions for the integration of OAuth 2.0.
6
5
  */
package/esm/create.js CHANGED
@@ -2,10 +2,10 @@
2
2
  * Creates new Pilet API extensions for the integration of OAuth 2.0.
3
3
  */
4
4
  export function createOAuth2Api(client) {
5
- return function (context) {
5
+ return (context) => {
6
6
  context.on('before-fetch', client.extendHeaders);
7
7
  return {
8
- getAccessToken: function () {
8
+ getAccessToken() {
9
9
  return client.token();
10
10
  },
11
11
  };
package/esm/create.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"create.js","sourceRoot":"","sources":["../src/create.ts"],"names":[],"mappings":"AAIA;;GAEG;AACH,MAAM,UAAU,eAAe,CAAC,MAAoB;IAClD,OAAO,UAAC,OAAO;QACb,OAAO,CAAC,EAAE,CAAC,cAAc,EAAE,MAAM,CAAC,aAAa,CAAC,CAAC;QAEjD,OAAO;YACL,cAAc;gBACZ,OAAO,MAAM,CAAC,KAAK,EAAE,CAAC;YACxB,CAAC;SACF,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC"}
1
+ {"version":3,"file":"create.js","sourceRoot":"","sources":["../src/create.ts"],"names":[],"mappings":"AAGA;;GAEG;AACH,MAAM,UAAU,eAAe,CAAC,MAAoB;IAClD,OAAO,CAAC,OAAO,EAAE,EAAE;QACjB,OAAO,CAAC,EAAE,CAAC,cAAc,EAAE,MAAM,CAAC,aAAa,CAAC,CAAC;QAEjD,OAAO;YACL,cAAc;gBACZ,OAAO,MAAM,CAAC,KAAK,EAAE,CAAC;YACxB,CAAC;SACF,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC"}
package/esm/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from './create';
2
2
  export * from './setup';
3
3
  export * from './types';
4
+ export * from './utils';
package/esm/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './create';
2
2
  export * from './setup';
3
3
  export * from './types';
4
+ export * from './utils';
4
5
  //# sourceMappingURL=index.js.map
package/esm/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC"}
package/esm/setup.d.ts CHANGED
@@ -1,72 +1,4 @@
1
- /**
2
- * Available configuration options for the OAuth 2.0 plugin.
3
- */
4
- export interface OAuth2Config {
5
- /**
6
- * The id of the client. Required for the setup of OAuth 2.0.
7
- */
8
- clientId: string;
9
- /**
10
- * The client secret. Only required for the `code` flow.
11
- */
12
- clientSecret?: string;
13
- /**
14
- * The Uri pointing to the authorization endpoint of the Identity Provider.
15
- */
16
- authorizationUri: string;
17
- /**
18
- * The Uri pointing to the access token endpoint of the Identity Provider.
19
- */
20
- accessTokenUri?: string;
21
- /**
22
- * The redirect Uri to use. By default the origin with /auth
23
- * is used.
24
- */
25
- redirectUri?: string;
26
- /**
27
- * The scopes to be used.
28
- */
29
- scopes?: Array<string>;
30
- /**
31
- * The OAuth 2.0 authorization flow type to be used.
32
- */
33
- flow?: 'implicit' | 'code';
34
- /**
35
- * Restricts token sharing such that other integrations, e.g., with
36
- * fetch would need to be done manually.
37
- * Otherwise, the client is responsive to the `before-fetch` event.
38
- */
39
- restrict?: boolean;
40
- }
41
- export interface OAuth2Request {
42
- /**
43
- * Sets the headers of the request.
44
- * @param headers Headers or a promise to headers.
45
- */
46
- setHeaders(headers: any): void;
47
- }
48
- export interface OAuth2Client {
49
- /**
50
- * Performs a login.
51
- */
52
- login(): void;
53
- /**
54
- * Performs a logout.
55
- */
56
- logout(): void;
57
- /**
58
- * Gets a token.
59
- */
60
- token(): Promise<string>;
61
- /**
62
- * Checks if the user is currently logged in.
63
- */
64
- account(): boolean;
65
- /**
66
- * Extends the headers of the provided request.
67
- */
68
- extendHeaders(req: OAuth2Request): void;
69
- }
1
+ import { OAuth2Config, OAuth2Client } from './types';
70
2
  /**
71
3
  * Sets up a new client wrapping the OAuth 2.0 API.
72
4
  * @param config The configuration for the client.
package/esm/setup.js CHANGED
@@ -1,77 +1,100 @@
1
- import * as ClientOAuth2 from 'client-oauth2';
2
- var callbackName = 'oauth2Cb';
1
+ import ClientOAuth2 from 'client-oauth2';
2
+ import { createOAuth2MemoryPersistence } from './utils';
3
+ const callbackName = 'oauth2Cb';
3
4
  /**
4
5
  * Sets up a new client wrapping the OAuth 2.0 API.
5
6
  * @param config The configuration for the client.
6
7
  */
7
8
  export function setupOAuth2Client(config) {
8
- var clientId = config.clientId, clientSecret = config.clientSecret, authorizationUri = config.authorizationUri, accessTokenUri = config.accessTokenUri, _a = config.redirectUri, redirectUri = _a === void 0 ? location.origin + "/auth" : _a, _b = config.scopes, scopes = _b === void 0 ? [] : _b, flow = config.flow, _c = config.restrict, restrict = _c === void 0 ? false : _c;
9
- var client = new ClientOAuth2({
10
- clientId: clientId,
11
- clientSecret: clientSecret,
12
- redirectUri: redirectUri,
13
- authorizationUri: authorizationUri,
14
- accessTokenUri: accessTokenUri,
15
- scopes: scopes,
9
+ const { clientId, clientSecret, authorizationUri, accessTokenUri, redirectUri = `${location.origin}/auth`, scopes = [], flow, headers, query, state, restrict = false, returnPath = '/', persist = createOAuth2MemoryPersistence(), } = config;
10
+ const client = new ClientOAuth2({
11
+ clientId,
12
+ clientSecret,
13
+ redirectUri,
14
+ authorizationUri,
15
+ accessTokenUri,
16
+ scopes,
17
+ headers,
18
+ query,
19
+ state,
16
20
  });
17
- var currentToken;
18
- var retrieveToken;
19
- var getLoginUri;
20
- if (flow === 'code') {
21
- client.code.getToken(location.href).then(function (token) { return (currentToken = token); }, function () { });
22
- retrieveToken = function () {
21
+ let currentToken;
22
+ let retrieveToken;
23
+ let getLoginUri;
24
+ const setCurrentToken = (token) => {
25
+ persist.save({
26
+ accessToken: token.accessToken,
27
+ data: token.data,
28
+ refreshToken: token.refreshToken,
29
+ });
30
+ currentToken = token;
31
+ };
32
+ const retrieve = (init, refresh) => {
33
+ return init.then(() => {
23
34
  if (!currentToken) {
24
35
  return Promise.reject('Not logged in. Please call `login()` to retrieve a token.');
25
36
  }
26
37
  if (!currentToken.expired()) {
27
- return Promise.resolve(currentToken.accessToken);
38
+ return currentToken.accessToken;
28
39
  }
29
- return currentToken.refresh().then(function (refreshedToken) {
30
- currentToken = refreshedToken;
40
+ return refresh().then((refreshedToken) => {
41
+ setCurrentToken(refreshedToken);
31
42
  return currentToken.accessToken;
32
43
  });
44
+ });
45
+ };
46
+ const initialize = (load) => {
47
+ const info = persist.load();
48
+ if (info) {
49
+ currentToken = client.createToken(info.accessToken, info.refreshToken, info.data);
50
+ return Promise.resolve();
51
+ }
52
+ else {
53
+ return load().then((token) => {
54
+ const opener = window.opener;
55
+ setCurrentToken(token);
56
+ if (opener && typeof opener[callbackName] === 'function') {
57
+ opener[callbackName](token);
58
+ window.close();
59
+ }
60
+ }, () => { });
61
+ }
62
+ };
63
+ if (flow === 'code') {
64
+ const init = initialize(() => {
65
+ const url = location.href;
66
+ history.replaceState(undefined, undefined, returnPath);
67
+ return client.code.getToken(url);
68
+ });
69
+ retrieveToken = () => {
70
+ return retrieve(init, () => currentToken.refresh());
33
71
  };
34
- getLoginUri = function () { return client.code.getUri(); };
72
+ getLoginUri = () => client.code.getUri();
35
73
  }
36
74
  else {
37
- client.token.getToken(location.href).then(function (token) {
38
- var opener = window.opener;
39
- if (opener && typeof opener[callbackName] === 'function') {
40
- opener[callbackName](token);
41
- window.close();
42
- }
43
- currentToken = token;
44
- }, function () { });
45
- retrieveToken = function () {
46
- if (!currentToken) {
47
- return Promise.reject('Not logged in. Please call `login()` to retrieve a token.');
48
- }
49
- if (!currentToken.expired()) {
50
- return Promise.resolve(currentToken.accessToken);
51
- }
52
- return new Promise(function (res) {
53
- window[callbackName] = function (token) {
54
- currentToken = token;
55
- res(currentToken.accessToken);
56
- };
75
+ const init = initialize(() => client.token.getToken(location.href));
76
+ retrieveToken = () => {
77
+ return retrieve(init, () => new Promise((resolve) => {
78
+ window[callbackName] = resolve;
57
79
  window.open(client.token.getUri());
58
- });
80
+ }));
59
81
  };
60
- getLoginUri = function () { return client.token.getUri(); };
82
+ getLoginUri = () => client.token.getUri();
61
83
  }
62
84
  return {
63
- login: function () {
85
+ _: client,
86
+ login() {
64
87
  window.location.href = getLoginUri();
65
88
  },
66
- logout: function () {
89
+ logout() {
67
90
  currentToken = undefined;
68
91
  },
69
- extendHeaders: function (req) {
92
+ extendHeaders(req) {
70
93
  if (!restrict) {
71
- req.setHeaders(retrieveToken().then(function (token) { return token && { Authorization: "Bearer " + token }; }, function () { return undefined; }));
94
+ req.setHeaders(retrieveToken().then((token) => token && { Authorization: `Bearer ${token}` }, () => undefined));
72
95
  }
73
96
  },
74
- account: function () {
97
+ account() {
75
98
  return !!currentToken;
76
99
  },
77
100
  token: retrieveToken,
package/esm/setup.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"setup.js","sourceRoot":"","sources":["../src/setup.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,YAAY,MAAM,eAAe,CAAC;AA0E9C,IAAM,YAAY,GAAG,UAAU,CAAC;AAEhC;;;GAGG;AACH,MAAM,UAAU,iBAAiB,CAAC,MAAoB;IAElD,IAAA,QAAQ,GAQN,MAAM,SARA,EACR,YAAY,GAOV,MAAM,aAPI,EACZ,gBAAgB,GAMd,MAAM,iBANQ,EAChB,cAAc,GAKZ,MAAM,eALM,EACd,KAIE,MAAM,YAJ+B,EAAvC,WAAW,mBAAM,QAAQ,CAAC,MAAM,UAAO,KAAA,EACvC,KAGE,MAAM,OAHG,EAAX,MAAM,mBAAG,EAAE,KAAA,EACX,IAAI,GAEF,MAAM,KAFJ,EACJ,KACE,MAAM,SADQ,EAAhB,QAAQ,mBAAG,KAAK,KAAA,CACP;IACX,IAAM,M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EAAE,CAAC,SAAS,CAChB,CACF,CAAC;aACH;QACH,CAAC;QACD,OAAO;YACL,OAAO,CAAC,CAAC,YAAY,CAAC;QACxB,CAAC;QACD,KAAK,EAAE,aAAa;KACrB,CAAC;AACJ,CAAC"}
package/esm/types.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { Data } from 'client-oauth2';
1
2
  declare module 'piral-core/lib/types/custom' {
2
3
  interface PiletCustomApi extends PiralOAuth2Api {
3
4
  }
@@ -8,3 +9,119 @@ export interface PiralOAuth2Api {
8
9
  */
9
10
  getAccessToken(): Promise<string | undefined>;
10
11
  }
12
+ /**
13
+ * The relevant OAuth 2 token information.
14
+ */
15
+ export interface OAuth2TokenInfo {
16
+ accessToken: string;
17
+ refreshToken: string;
18
+ data: Data;
19
+ }
20
+ /**
21
+ * Available configuration options for the OAuth 2 plugin.
22
+ */
23
+ export interface OAuth2Config {
24
+ /**
25
+ * The id of the client. Required for the setup of OAuth 2.
26
+ */
27
+ clientId: string;
28
+ /**
29
+ * The client secret. Only required for the `code` flow.
30
+ */
31
+ clientSecret?: string;
32
+ /**
33
+ * The Uri pointing to the authorization endpoint of the Identity Provider.
34
+ */
35
+ authorizationUri: string;
36
+ /**
37
+ * The Uri pointing to the access token endpoint of the Identity Provider.
38
+ */
39
+ accessTokenUri?: string;
40
+ /**
41
+ * The redirect Uri to use. By default the origin with /auth
42
+ * is used.
43
+ */
44
+ redirectUri?: string;
45
+ /**
46
+ * The return path to use in case of the "code" flow. By default the
47
+ * path will be set to "/".
48
+ */
49
+ returnPath?: string;
50
+ /**
51
+ * The scopes to be used.
52
+ */
53
+ scopes?: Array<string>;
54
+ /**
55
+ * The OAuth 2 authorization flow type to be used.
56
+ */
57
+ flow?: 'implicit' | 'code';
58
+ /**
59
+ * Restricts token sharing such that other integrations, e.g., with
60
+ * fetch would need to be done manually.
61
+ * Otherwise, the client is responsive to the `before-fetch` event.
62
+ */
63
+ restrict?: boolean;
64
+ /**
65
+ * Optional persistence layer for OAuth 2. By default nothing is stored.
66
+ */
67
+ persist?: OAuth2Persistence;
68
+ /**
69
+ * The optional headers to supply in OAuth 2 requests.
70
+ */
71
+ headers?: Record<string, string | Array<string>>;
72
+ /**
73
+ * The optional query parameters to supply in OAuth 2 requests.
74
+ */
75
+ query?: Record<string, string | Array<string>>;
76
+ /**
77
+ * The optional state parameter to supply in OAuth 2 requests.
78
+ */
79
+ state?: string;
80
+ }
81
+ export interface OAuth2Request {
82
+ /**
83
+ * Sets the headers of the request.
84
+ * @param headers Headers or a promise to headers.
85
+ */
86
+ setHeaders(headers: any): void;
87
+ }
88
+ export interface OAuth2Client {
89
+ /**
90
+ * The underlying OAuth2 client.
91
+ */
92
+ _: any;
93
+ /**
94
+ * Performs a login.
95
+ */
96
+ login(): void;
97
+ /**
98
+ * Performs a logout.
99
+ */
100
+ logout(): void;
101
+ /**
102
+ * Gets a token.
103
+ */
104
+ token(): Promise<string>;
105
+ /**
106
+ * Checks if the user is currently logged in.
107
+ */
108
+ account(): boolean;
109
+ /**
110
+ * Extends the headers of the provided request.
111
+ */
112
+ extendHeaders(req: OAuth2Request): void;
113
+ }
114
+ /**
115
+ * Defines the interface for the OAuth 2 persistence layer.
116
+ */
117
+ export interface OAuth2Persistence {
118
+ /**
119
+ * Loads an OAuth 2 token structure.
120
+ */
121
+ load(): OAuth2TokenInfo;
122
+ /**
123
+ * Stores an OAuth 2 token structure.
124
+ * @param info The token infos to store.
125
+ */
126
+ save(info: OAuth2TokenInfo): void;
127
+ }
package/esm/utils.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ import { OAuth2Persistence } from './types';
2
+ /**
3
+ * Creates an OAuth 2 persistence layer using memory.
4
+ */
5
+ export declare function createOAuth2MemoryPersistence(): OAuth2Persistence;
6
+ /**
7
+ * Creates an OAuth 2 persistence layer using sessionStorage.
8
+ */
9
+ export declare function createOAuth2SessionPersistence(sessionKey?: string): OAuth2Persistence;
10
+ /**
11
+ * Creates an OAuth 2 persistence layer using localStorage.
12
+ */
13
+ export declare function createOAuth2BrowserPersistence(localKey?: string): OAuth2Persistence;
package/esm/utils.js ADDED
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Creates an OAuth 2 persistence layer using memory.
3
+ */
4
+ export function createOAuth2MemoryPersistence() {
5
+ return {
6
+ load() {
7
+ return undefined;
8
+ },
9
+ save() { },
10
+ };
11
+ }
12
+ /**
13
+ * Creates an OAuth 2 persistence layer using sessionStorage.
14
+ */
15
+ export function createOAuth2SessionPersistence(sessionKey = '$piral_oauth2_info') {
16
+ return {
17
+ load() {
18
+ const content = sessionStorage.getItem(sessionKey);
19
+ if (typeof content === 'string') {
20
+ try {
21
+ return JSON.parse(content);
22
+ }
23
+ catch (_a) {
24
+ console.error('Found invalid data in the OAuth 2 session storage key. Skipped.');
25
+ }
26
+ }
27
+ return undefined;
28
+ },
29
+ save(info) {
30
+ sessionStorage.setItem(sessionKey, JSON.stringify(info));
31
+ },
32
+ };
33
+ }
34
+ /**
35
+ * Creates an OAuth 2 persistence layer using localStorage.
36
+ */
37
+ export function createOAuth2BrowserPersistence(localKey = '$piral_oauth2_info') {
38
+ return {
39
+ load() {
40
+ const content = localStorage.getItem(localKey);
41
+ if (typeof content === 'string') {
42
+ try {
43
+ return JSON.parse(content);
44
+ }
45
+ catch (_a) {
46
+ console.error('Found invalid data in the OAuth 2 local storage key. Skipped.');
47
+ }
48
+ }
49
+ return undefined;
50
+ },
51
+ save(info) {
52
+ localStorage.setItem(localKey, JSON.stringify(info));
53
+ },
54
+ };
55
+ }
56
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,UAAU,6BAA6B;IAC3C,OAAO;QACL,IAAI;YACF,OAAO,SAAS,CAAC;QACnB,CAAC;QACD,IAAI,KAAI,CAAC;KACV,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,8BAA8B,CAAC,UAAU,GAAG,oBAAoB;IAC9E,OAAO;QACL,IAAI;YACF,MAAM,OAAO,GAAG,cAAc,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;YAEnD,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE;gBAC/B,IAAI;oBACF,OAAO,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;iBAC5B;gBAAC,WAAM;oBACN,OAAO,CAAC,KAAK,CAAC,iEAAiE,CAAC,CAAC;iBAClF;aACF;YAED,OAAO,SAAS,CAAC;QACnB,CAAC;QACD,IAAI,CAAC,IAAI;YACP,cAAc,CAAC,OAAO,CAAC,UAAU,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC;QAC3D,CAAC;KACF,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,8BAA8B,CAAC,QAAQ,GAAG,oBAAoB;IAC5E,OAAO;QACL,IAAI;YACF,MAAM,OAAO,GAAG,YAAY,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;YAE/C,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE;gBAC/B,IAAI;oBACF,OAAO,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;iBAC5B;gBAAC,WAAM;oBACN,OAAO,CAAC,KAAK,CAAC,+DAA+D,CAAC,CAAC;iBAChF;aACF;YAED,OAAO,SAAS,CAAC;QACnB,CAAC;QACD,IAAI,CAAC,IAAI;YACP,YAAY,CAAC,OAAO,CAAC,QAAQ,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC;QACvD,CAAC;KACF,CAAC;AACJ,CAAC"}
package/lib/create.d.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import { PiralPlugin } from 'piral-core';
2
- import { OAuth2Client } from './setup';
3
- import { PiralOAuth2Api } from './types';
2
+ import { OAuth2Client, PiralOAuth2Api } from './types';
4
3
  /**
5
4
  * Creates new Pilet API extensions for the integration of OAuth 2.0.
6
5
  */
package/lib/create.js CHANGED
@@ -5,10 +5,10 @@ exports.createOAuth2Api = void 0;
5
5
  * Creates new Pilet API extensions for the integration of OAuth 2.0.
6
6
  */
7
7
  function createOAuth2Api(client) {
8
- return function (context) {
8
+ return (context) => {
9
9
  context.on('before-fetch', client.extendHeaders);
10
10
  return {
11
- getAccessToken: function () {
11
+ getAccessToken() {
12
12
  return client.token();
13
13
  },
14
14
  };
package/lib/create.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"create.js","sourceRoot":"","sources":["../src/create.ts"],"names":[],"mappings":";;;AAIA;;GAEG;AACH,SAAgB,eAAe,CAAC,MAAoB;IAClD,OAAO,UAAC,OAAO;QACb,OAAO,CAAC,EAAE,CAAC,cAAc,EAAE,MAAM,CAAC,aAAa,CAAC,CAAC;QAEjD,OAAO;YACL,cAAc;gBACZ,OAAO,MAAM,CAAC,KAAK,EAAE,CAAC;YACxB,CAAC;SACF,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC;AAVD,0CAUC"}
1
+ {"version":3,"file":"create.js","sourceRoot":"","sources":["../src/create.ts"],"names":[],"mappings":";;;AAGA;;GAEG;AACH,SAAgB,eAAe,CAAC,MAAoB;IAClD,OAAO,CAAC,OAAO,EAAE,EAAE;QACjB,OAAO,CAAC,EAAE,CAAC,cAAc,EAAE,MAAM,CAAC,aAAa,CAAC,CAAC;QAEjD,OAAO;YACL,cAAc;gBACZ,OAAO,MAAM,CAAC,KAAK,EAAE,CAAC;YACxB,CAAC;SACF,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC;AAVD,0CAUC"}
package/lib/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from './create';
2
2
  export * from './setup';
3
3
  export * from './types';
4
+ export * from './utils';
package/lib/index.js CHANGED
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- var tslib_1 = require("tslib");
3
+ const tslib_1 = require("tslib");
4
4
  tslib_1.__exportStar(require("./create"), exports);
5
5
  tslib_1.__exportStar(require("./setup"), exports);
6
6
  tslib_1.__exportStar(require("./types"), exports);
7
+ tslib_1.__exportStar(require("./utils"), exports);
7
8
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,mDAAyB;AACzB,kDAAwB;AACxB,kDAAwB"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,mDAAyB;AACzB,kDAAwB;AACxB,kDAAwB;AACxB,kDAAwB"}