piral-oauth2 0.14.1-beta.3256 → 0.14.2

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/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.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,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"}
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/setup.d.ts CHANGED
@@ -1,82 +1,4 @@
1
- import { OAuth2Persistence } from './types';
2
- /**
3
- * Available configuration options for the OAuth 2 plugin.
4
- */
5
- export interface OAuth2Config {
6
- /**
7
- * The id of the client. Required for the setup of OAuth 2.
8
- */
9
- clientId: string;
10
- /**
11
- * The client secret. Only required for the `code` flow.
12
- */
13
- clientSecret?: string;
14
- /**
15
- * The Uri pointing to the authorization endpoint of the Identity Provider.
16
- */
17
- authorizationUri: string;
18
- /**
19
- * The Uri pointing to the access token endpoint of the Identity Provider.
20
- */
21
- accessTokenUri?: string;
22
- /**
23
- * The redirect Uri to use. By default the origin with /auth
24
- * is used.
25
- */
26
- redirectUri?: string;
27
- /**
28
- * The return path to use in case of the "code" flow. By default the
29
- * path will be set to "/".
30
- */
31
- returnPath?: string;
32
- /**
33
- * The scopes to be used.
34
- */
35
- scopes?: Array<string>;
36
- /**
37
- * The OAuth 2 authorization flow type to be used.
38
- */
39
- flow?: 'implicit' | 'code';
40
- /**
41
- * Restricts token sharing such that other integrations, e.g., with
42
- * fetch would need to be done manually.
43
- * Otherwise, the client is responsive to the `before-fetch` event.
44
- */
45
- restrict?: boolean;
46
- /**
47
- * Optional persistence layer for OAuth 2. By default nothing is stored.
48
- */
49
- persist?: OAuth2Persistence;
50
- }
51
- export interface OAuth2Request {
52
- /**
53
- * Sets the headers of the request.
54
- * @param headers Headers or a promise to headers.
55
- */
56
- setHeaders(headers: any): void;
57
- }
58
- export interface OAuth2Client {
59
- /**
60
- * Performs a login.
61
- */
62
- login(): void;
63
- /**
64
- * Performs a logout.
65
- */
66
- logout(): void;
67
- /**
68
- * Gets a token.
69
- */
70
- token(): Promise<string>;
71
- /**
72
- * Checks if the user is currently logged in.
73
- */
74
- account(): boolean;
75
- /**
76
- * Extends the headers of the provided request.
77
- */
78
- extendHeaders(req: OAuth2Request): void;
79
- }
1
+ import { OAuth2Config, OAuth2Client } from './types';
80
2
  /**
81
3
  * Sets up a new client wrapping the OAuth 2.0 API.
82
4
  * @param config The configuration for the client.
package/esm/setup.js CHANGED
@@ -6,7 +6,7 @@ const callbackName = 'oauth2Cb';
6
6
  * @param config The configuration for the client.
7
7
  */
8
8
  export function setupOAuth2Client(config) {
9
- const { clientId, clientSecret, authorizationUri, accessTokenUri, redirectUri = `${location.origin}/auth`, scopes = [], flow, restrict = false, returnPath = '/', persist = createOAuth2MemoryPersistence(), } = config;
9
+ const { clientId, clientSecret, authorizationUri, accessTokenUri, redirectUri = `${location.origin}/auth`, scopes = [], flow, headers, query, state, restrict = false, returnPath = '/', persist = createOAuth2MemoryPersistence(), } = config;
10
10
  const client = new ClientOAuth2({
11
11
  clientId,
12
12
  clientSecret,
@@ -14,6 +14,9 @@ export function setupOAuth2Client(config) {
14
14
  authorizationUri,
15
15
  accessTokenUri,
16
16
  scopes,
17
+ headers,
18
+ query,
19
+ state,
17
20
  });
18
21
  let currentToken;
19
22
  let retrieveToken;
@@ -79,6 +82,7 @@ export function setupOAuth2Client(config) {
79
82
  getLoginUri = () => client.token.getUri();
80
83
  }
81
84
  return {
85
+ _: client,
82
86
  login() {
83
87
  window.location.href = getLoginUri();
84
88
  },
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;AAC9C,OAAO,EAAE,6BAA6B,EAAE,MAAM,SAAS,CAAC;AAoFxD,MAAM,YAAY,GAAG,UAAU,CAAC;AAEhC;;;GAGG;AACH,MAAM,UAAU,iBAAiB,CAAC,MAAoB;IACpD,MAAM,EACJ,QAAQ,EACR,YAAY,EACZ,gBAAgB,EAChB,cAAc,EACd,WAAW,GAAG,GAAG,QAAQ,CAAC,MAAM,OAAO,EACvC,MAAM,GAAG,EAAE,EACX,IAAI,EACJ,QAAQ,GAAG,KAAK,EAChB,UAAU,GAAG,GAAG,EAChB,OAAO,GAAG,6BAA6B,EAAE,GAC1C,GAAG,MAAM,CAAC;IACX,MAAM,MAAM,GAAG,IAAI,YAAY,CAAC;QAC9B,QAAQ;QACR,YAAY;QACZ,WAAW;QACX,gBAAgB;QAChB,cAAc;QACd,MAAM;KACP,CAAC,CAAC;IACH,IAAI,YAAgC,CAAC;IACrC,IAAI,aAAo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M,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC;YACrC,CAAC,CAAC,CACL,CAAC;QACJ,CAAC,CAAC;QACF,WAAW,GAAG,GAAG,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC;KAC3C;IAED,OAAO;QACL,CAAC,EAAE,MAAM;QACT,KAAK;YACH,MAAM,CAAC,QAAQ,CAAC,IAAI,GAAG,WAAW,EAAE,CAAC;QACvC,CAAC;QACD,MAAM;YACJ,YAAY,GAAG,SAAS,CAAC;QAC3B,CAAC;QACD,aAAa,CAAC,GAAG;YACf,IAAI,CAAC,QAAQ,EAAE;gBACb,GAAG,CAAC,UAAU,CACZ,aAAa,EAAE,CAAC,IAAI,CAClB,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,IAAI,EAAE,aAAa,EAAE,UAAU,KAAK,EAAE,EAAE,EACxD,GAAG,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
@@ -17,6 +17,100 @@ export interface OAuth2TokenInfo {
17
17
  refreshToken: string;
18
18
  data: Data;
19
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
+ }
20
114
  /**
21
115
  * Defines the interface for the OAuth 2 persistence layer.
22
116
  */
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.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,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"}
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/setup.d.ts CHANGED
@@ -1,82 +1,4 @@
1
- import { OAuth2Persistence } from './types';
2
- /**
3
- * Available configuration options for the OAuth 2 plugin.
4
- */
5
- export interface OAuth2Config {
6
- /**
7
- * The id of the client. Required for the setup of OAuth 2.
8
- */
9
- clientId: string;
10
- /**
11
- * The client secret. Only required for the `code` flow.
12
- */
13
- clientSecret?: string;
14
- /**
15
- * The Uri pointing to the authorization endpoint of the Identity Provider.
16
- */
17
- authorizationUri: string;
18
- /**
19
- * The Uri pointing to the access token endpoint of the Identity Provider.
20
- */
21
- accessTokenUri?: string;
22
- /**
23
- * The redirect Uri to use. By default the origin with /auth
24
- * is used.
25
- */
26
- redirectUri?: string;
27
- /**
28
- * The return path to use in case of the "code" flow. By default the
29
- * path will be set to "/".
30
- */
31
- returnPath?: string;
32
- /**
33
- * The scopes to be used.
34
- */
35
- scopes?: Array<string>;
36
- /**
37
- * The OAuth 2 authorization flow type to be used.
38
- */
39
- flow?: 'implicit' | 'code';
40
- /**
41
- * Restricts token sharing such that other integrations, e.g., with
42
- * fetch would need to be done manually.
43
- * Otherwise, the client is responsive to the `before-fetch` event.
44
- */
45
- restrict?: boolean;
46
- /**
47
- * Optional persistence layer for OAuth 2. By default nothing is stored.
48
- */
49
- persist?: OAuth2Persistence;
50
- }
51
- export interface OAuth2Request {
52
- /**
53
- * Sets the headers of the request.
54
- * @param headers Headers or a promise to headers.
55
- */
56
- setHeaders(headers: any): void;
57
- }
58
- export interface OAuth2Client {
59
- /**
60
- * Performs a login.
61
- */
62
- login(): void;
63
- /**
64
- * Performs a logout.
65
- */
66
- logout(): void;
67
- /**
68
- * Gets a token.
69
- */
70
- token(): Promise<string>;
71
- /**
72
- * Checks if the user is currently logged in.
73
- */
74
- account(): boolean;
75
- /**
76
- * Extends the headers of the provided request.
77
- */
78
- extendHeaders(req: OAuth2Request): void;
79
- }
1
+ import { OAuth2Config, OAuth2Client } from './types';
80
2
  /**
81
3
  * Sets up a new client wrapping the OAuth 2.0 API.
82
4
  * @param config The configuration for the client.
package/lib/setup.js CHANGED
@@ -9,7 +9,7 @@ const callbackName = 'oauth2Cb';
9
9
  * @param config The configuration for the client.
10
10
  */
11
11
  function setupOAuth2Client(config) {
12
- const { clientId, clientSecret, authorizationUri, accessTokenUri, redirectUri = `${location.origin}/auth`, scopes = [], flow, restrict = false, returnPath = '/', persist = (0, utils_1.createOAuth2MemoryPersistence)(), } = config;
12
+ const { clientId, clientSecret, authorizationUri, accessTokenUri, redirectUri = `${location.origin}/auth`, scopes = [], flow, headers, query, state, restrict = false, returnPath = '/', persist = (0, utils_1.createOAuth2MemoryPersistence)(), } = config;
13
13
  const client = new ClientOAuth2({
14
14
  clientId,
15
15
  clientSecret,
@@ -17,6 +17,9 @@ function setupOAuth2Client(config) {
17
17
  authorizationUri,
18
18
  accessTokenUri,
19
19
  scopes,
20
+ headers,
21
+ query,
22
+ state,
20
23
  });
21
24
  let currentToken;
22
25
  let retrieveToken;
@@ -82,6 +85,7 @@ function setupOAuth2Client(config) {
82
85
  getLoginUri = () => client.token.getUri();
83
86
  }
84
87
  return {
88
+ _: client,
85
89
  login() {
86
90
  window.location.href = getLoginUri();
87
91
  },
package/lib/setup.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"setup.js","sourceRoot":"","sources":["../src/setup.ts"],"names":[],"mappings":";;;AAAA,8CAA8C;AAC9C,mCAAwD;AAoFxD,MAAM,YAAY,GAAG,UAAU,CAAC;AAEhC;;;GAGG;AACH,SAAgB,iBAAiB,CAAC,MAAoB;IACpD,MAAM,EACJ,QAAQ,EACR,YAAY,EACZ,gBAAgB,EAChB,cAAc,EACd,WAAW,GAAG,GAAG,QAAQ,CAAC,MAAM,OAAO,EACvC,MAAM,GAAG,EAAE,EACX,IAAI,EACJ,QAAQ,GAAG,KAAK,EAChB,UAAU,GAAG,GAAG,EAChB,OAAO,GAAG,IAAA,qCAA6B,GAAE,GAC1C,GAAG,MAAM,CAAC;IACX,MAAM,MAAM,GAAG,IAAI,YAAY,CAAC;QAC9B,QAAQ;QACR,YAAY;QACZ,WAAW;QACX,gBAAgB;QAChB,cAAc;QACd,MAAM;KACP,CAAC,CAAC;IACH,IAAI,YAAgC,CAAC;IACrC,IAAI,aAAoC,CAAC;IACzC,IAAI,WAAyB,CAAC;IAE9B,MAAM,eAAe,GAAG,CAAC,KAAyB,EAAE,EAAE;QACpD,OAAO,CAAC,IAAI,CAAC;YACX,WAAW,EAAE,KAAK,CAAC,WAAW;YAC9B,IAAI,EAAE,KAAK,CAAC,IAAI;YAChB,YAAY,EAAE,KAAK,CAAC,YAAY;SACjC,CAAC,CAAC;QAEH,YAAY,GAAG,KAAK,CAAC;IACvB,CAAC,CAAC;IAEF,M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CAAC;QACD,aAAa,CAAC,GAAG;YACf,IAAI,CAAC,QAAQ,EAAE;gBACb,GAAG,CAAC,UAAU,CACZ,aAAa,EAAE,CAAC,IAAI,CAClB,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,IAAI,EAAE,aAAa,EAAE,UAAU,KAAK,EAAE,EAAE,EACxD,GAAG,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;AArID,8CAqIC"}
package/lib/types.d.ts CHANGED
@@ -17,6 +17,100 @@ export interface OAuth2TokenInfo {
17
17
  refreshToken: string;
18
18
  data: Data;
19
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
+ }
20
114
  /**
21
115
  * Defines the interface for the OAuth 2 persistence layer.
22
116
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "piral-oauth2",
3
- "version": "0.14.1-beta.3256",
3
+ "version": "0.14.2",
4
4
  "description": "Plugin to integrate OAuth 2.0 authentication in Piral.",
5
5
  "keywords": [
6
6
  "piral",
@@ -46,10 +46,10 @@
46
46
  "client-oauth2": "^4.2.5"
47
47
  },
48
48
  "devDependencies": {
49
- "piral-core": "0.14.1-beta.3256"
49
+ "piral-core": "^0.14.2"
50
50
  },
51
51
  "peerDependencies": {
52
52
  "piral-core": "0.14.x"
53
53
  },
54
- "gitHead": "4fe700c9466e6c3920546713e911fef36c311b52"
54
+ "gitHead": "eaaab0f56b872802a70d83de27a77911af37ed22"
55
55
  }
package/src/create.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
  /**
6
5
  * Creates new Pilet API extensions for the integration of OAuth 2.0.
package/src/setup.ts CHANGED
@@ -1,87 +1,6 @@
1
1
  import * as ClientOAuth2 from 'client-oauth2';
2
2
  import { createOAuth2MemoryPersistence } from './utils';
3
- import { OAuth2Persistence } from './types';
4
-
5
- /**
6
- * Available configuration options for the OAuth 2 plugin.
7
- */
8
- export interface OAuth2Config {
9
- /**
10
- * The id of the client. Required for the setup of OAuth 2.
11
- */
12
- clientId: string;
13
- /**
14
- * The client secret. Only required for the `code` flow.
15
- */
16
- clientSecret?: string;
17
- /**
18
- * The Uri pointing to the authorization endpoint of the Identity Provider.
19
- */
20
- authorizationUri: string;
21
- /**
22
- * The Uri pointing to the access token endpoint of the Identity Provider.
23
- */
24
- accessTokenUri?: string;
25
- /**
26
- * The redirect Uri to use. By default the origin with /auth
27
- * is used.
28
- */
29
- redirectUri?: string;
30
- /**
31
- * The return path to use in case of the "code" flow. By default the
32
- * path will be set to "/".
33
- */
34
- returnPath?: string;
35
- /**
36
- * The scopes to be used.
37
- */
38
- scopes?: Array<string>;
39
- /**
40
- * The OAuth 2 authorization flow type to be used.
41
- */
42
- flow?: 'implicit' | 'code';
43
- /**
44
- * Restricts token sharing such that other integrations, e.g., with
45
- * fetch would need to be done manually.
46
- * Otherwise, the client is responsive to the `before-fetch` event.
47
- */
48
- restrict?: boolean;
49
- /**
50
- * Optional persistence layer for OAuth 2. By default nothing is stored.
51
- */
52
- persist?: OAuth2Persistence;
53
- }
54
-
55
- export interface OAuth2Request {
56
- /**
57
- * Sets the headers of the request.
58
- * @param headers Headers or a promise to headers.
59
- */
60
- setHeaders(headers: any): void;
61
- }
62
-
63
- export interface OAuth2Client {
64
- /**
65
- * Performs a login.
66
- */
67
- login(): void;
68
- /**
69
- * Performs a logout.
70
- */
71
- logout(): void;
72
- /**
73
- * Gets a token.
74
- */
75
- token(): Promise<string>;
76
- /**
77
- * Checks if the user is currently logged in.
78
- */
79
- account(): boolean;
80
- /**
81
- * Extends the headers of the provided request.
82
- */
83
- extendHeaders(req: OAuth2Request): void;
84
- }
3
+ import { OAuth2Config, OAuth2Client } from './types';
85
4
 
86
5
  const callbackName = 'oauth2Cb';
87
6
 
@@ -98,10 +17,14 @@ export function setupOAuth2Client(config: OAuth2Config): OAuth2Client {
98
17
  redirectUri = `${location.origin}/auth`,
99
18
  scopes = [],
100
19
  flow,
20
+ headers,
21
+ query,
22
+ state,
101
23
  restrict = false,
102
24
  returnPath = '/',
103
25
  persist = createOAuth2MemoryPersistence(),
104
26
  } = config;
27
+
105
28
  const client = new ClientOAuth2({
106
29
  clientId,
107
30
  clientSecret,
@@ -109,7 +32,11 @@ export function setupOAuth2Client(config: OAuth2Config): OAuth2Client {
109
32
  authorizationUri,
110
33
  accessTokenUri,
111
34
  scopes,
35
+ headers,
36
+ query,
37
+ state,
112
38
  });
39
+
113
40
  let currentToken: ClientOAuth2.Token;
114
41
  let retrieveToken: () => Promise<string>;
115
42
  let getLoginUri: () => string;
@@ -192,6 +119,7 @@ export function setupOAuth2Client(config: OAuth2Config): OAuth2Client {
192
119
  }
193
120
 
194
121
  return {
122
+ _: client,
195
123
  login() {
196
124
  window.location.href = getLoginUri();
197
125
  },
package/src/types.ts CHANGED
@@ -20,6 +20,103 @@ export interface OAuth2TokenInfo {
20
20
  data: Data;
21
21
  }
22
22
 
23
+ /**
24
+ * Available configuration options for the OAuth 2 plugin.
25
+ */
26
+ export interface OAuth2Config {
27
+ /**
28
+ * The id of the client. Required for the setup of OAuth 2.
29
+ */
30
+ clientId: string;
31
+ /**
32
+ * The client secret. Only required for the `code` flow.
33
+ */
34
+ clientSecret?: string;
35
+ /**
36
+ * The Uri pointing to the authorization endpoint of the Identity Provider.
37
+ */
38
+ authorizationUri: string;
39
+ /**
40
+ * The Uri pointing to the access token endpoint of the Identity Provider.
41
+ */
42
+ accessTokenUri?: string;
43
+ /**
44
+ * The redirect Uri to use. By default the origin with /auth
45
+ * is used.
46
+ */
47
+ redirectUri?: string;
48
+ /**
49
+ * The return path to use in case of the "code" flow. By default the
50
+ * path will be set to "/".
51
+ */
52
+ returnPath?: string;
53
+ /**
54
+ * The scopes to be used.
55
+ */
56
+ scopes?: Array<string>;
57
+ /**
58
+ * The OAuth 2 authorization flow type to be used.
59
+ */
60
+ flow?: 'implicit' | 'code';
61
+ /**
62
+ * Restricts token sharing such that other integrations, e.g., with
63
+ * fetch would need to be done manually.
64
+ * Otherwise, the client is responsive to the `before-fetch` event.
65
+ */
66
+ restrict?: boolean;
67
+ /**
68
+ * Optional persistence layer for OAuth 2. By default nothing is stored.
69
+ */
70
+ persist?: OAuth2Persistence;
71
+ /**
72
+ * The optional headers to supply in OAuth 2 requests.
73
+ */
74
+ headers?: Record<string, string | Array<string>>;
75
+ /**
76
+ * The optional query parameters to supply in OAuth 2 requests.
77
+ */
78
+ query?: Record<string, string | Array<string>>;
79
+ /**
80
+ * The optional state parameter to supply in OAuth 2 requests.
81
+ */
82
+ state?: string;
83
+ }
84
+
85
+ export interface OAuth2Request {
86
+ /**
87
+ * Sets the headers of the request.
88
+ * @param headers Headers or a promise to headers.
89
+ */
90
+ setHeaders(headers: any): void;
91
+ }
92
+
93
+ export interface OAuth2Client {
94
+ /**
95
+ * The underlying OAuth2 client.
96
+ */
97
+ _: any;
98
+ /**
99
+ * Performs a login.
100
+ */
101
+ login(): void;
102
+ /**
103
+ * Performs a logout.
104
+ */
105
+ logout(): void;
106
+ /**
107
+ * Gets a token.
108
+ */
109
+ token(): Promise<string>;
110
+ /**
111
+ * Checks if the user is currently logged in.
112
+ */
113
+ account(): boolean;
114
+ /**
115
+ * Extends the headers of the provided request.
116
+ */
117
+ extendHeaders(req: OAuth2Request): void;
118
+ }
119
+
23
120
  /**
24
121
  * Defines the interface for the OAuth 2 persistence layer.
25
122
  */