@celljs/oauth2-client 3.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.
Files changed (249) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1 -0
  3. package/cell.yml +57 -0
  4. package/lib/node/authentication/authentication-protocol.d.ts +25 -0
  5. package/lib/node/authentication/authentication-protocol.d.ts.map +1 -0
  6. package/lib/node/authentication/authentication-protocol.js +11 -0
  7. package/lib/node/authentication/authentication-protocol.js.map +1 -0
  8. package/lib/node/authentication/index.d.ts +4 -0
  9. package/lib/node/authentication/index.d.ts.map +1 -0
  10. package/lib/node/authentication/index.js +20 -0
  11. package/lib/node/authentication/index.js.map +1 -0
  12. package/lib/node/authentication/oauth2-authentication-provider.d.ts +30 -0
  13. package/lib/node/authentication/oauth2-authentication-provider.d.ts.map +1 -0
  14. package/lib/node/authentication/oauth2-authentication-provider.js +170 -0
  15. package/lib/node/authentication/oauth2-authentication-provider.js.map +1 -0
  16. package/lib/node/authentication/oidc-authentication-provider.d.ts +18 -0
  17. package/lib/node/authentication/oidc-authentication-provider.d.ts.map +1 -0
  18. package/lib/node/authentication/oidc-authentication-provider.js +74 -0
  19. package/lib/node/authentication/oidc-authentication-provider.js.map +1 -0
  20. package/lib/node/authorization/authorization-code-authorized-client-provider.d.ts +5 -0
  21. package/lib/node/authorization/authorization-code-authorized-client-provider.d.ts.map +1 -0
  22. package/lib/node/authorization/authorization-code-authorized-client-provider.js +27 -0
  23. package/lib/node/authorization/authorization-code-authorized-client-provider.js.map +1 -0
  24. package/lib/node/authorization/authorization-code-grant-middleware.d.ts +26 -0
  25. package/lib/node/authorization/authorization-code-grant-middleware.d.ts.map +1 -0
  26. package/lib/node/authorization/authorization-code-grant-middleware.js +129 -0
  27. package/lib/node/authorization/authorization-code-grant-middleware.js.map +1 -0
  28. package/lib/node/authorization/authorization-failure-handler.d.ts +10 -0
  29. package/lib/node/authorization/authorization-failure-handler.d.ts.map +1 -0
  30. package/lib/node/authorization/authorization-failure-handler.js +35 -0
  31. package/lib/node/authorization/authorization-failure-handler.js.map +1 -0
  32. package/lib/node/authorization/authorization-protocol.d.ts +78 -0
  33. package/lib/node/authorization/authorization-protocol.d.ts.map +1 -0
  34. package/lib/node/authorization/authorization-protocol.js +34 -0
  35. package/lib/node/authorization/authorization-protocol.js.map +1 -0
  36. package/lib/node/authorization/authorization-request-manager.d.ts +13 -0
  37. package/lib/node/authorization/authorization-request-manager.d.ts.map +1 -0
  38. package/lib/node/authorization/authorization-request-manager.js +64 -0
  39. package/lib/node/authorization/authorization-request-manager.js.map +1 -0
  40. package/lib/node/authorization/authorization-request-redirect-middleware.d.ts +20 -0
  41. package/lib/node/authorization/authorization-request-redirect-middleware.d.ts.map +1 -0
  42. package/lib/node/authorization/authorization-request-redirect-middleware.js +113 -0
  43. package/lib/node/authorization/authorization-request-redirect-middleware.js.map +1 -0
  44. package/lib/node/authorization/authorization-request-resolver.d.ts +38 -0
  45. package/lib/node/authorization/authorization-request-resolver.d.ts.map +1 -0
  46. package/lib/node/authorization/authorization-request-resolver.js +155 -0
  47. package/lib/node/authorization/authorization-request-resolver.js.map +1 -0
  48. package/lib/node/authorization/authorization-success-handler.d.ts +7 -0
  49. package/lib/node/authorization/authorization-success-handler.d.ts.map +1 -0
  50. package/lib/node/authorization/authorization-success-handler.js +28 -0
  51. package/lib/node/authorization/authorization-success-handler.js.map +1 -0
  52. package/lib/node/authorization/authorized-client-manager.d.ts +11 -0
  53. package/lib/node/authorization/authorized-client-manager.d.ts.map +1 -0
  54. package/lib/node/authorization/authorized-client-manager.js +48 -0
  55. package/lib/node/authorization/authorized-client-manager.js.map +1 -0
  56. package/lib/node/authorization/authorized-client-service.d.ts +12 -0
  57. package/lib/node/authorization/authorized-client-service.d.ts.map +1 -0
  58. package/lib/node/authorization/authorized-client-service.js +87 -0
  59. package/lib/node/authorization/authorized-client-service.js.map +1 -0
  60. package/lib/node/authorization/client-credentials-authorized-client-provider.d.ts +12 -0
  61. package/lib/node/authorization/client-credentials-authorized-client-provider.d.ts.map +1 -0
  62. package/lib/node/authorization/client-credentials-authorized-client-provider.js +72 -0
  63. package/lib/node/authorization/client-credentials-authorized-client-provider.js.map +1 -0
  64. package/lib/node/authorization/context-attributes-mapper.d.ts +5 -0
  65. package/lib/node/authorization/context-attributes-mapper.d.ts.map +1 -0
  66. package/lib/node/authorization/context-attributes-mapper.js +28 -0
  67. package/lib/node/authorization/context-attributes-mapper.js.map +1 -0
  68. package/lib/node/authorization/index.d.ts +14 -0
  69. package/lib/node/authorization/index.d.ts.map +1 -0
  70. package/lib/node/authorization/index.js +30 -0
  71. package/lib/node/authorization/index.js.map +1 -0
  72. package/lib/node/authorization/password-authorized-client-provider.d.ts +12 -0
  73. package/lib/node/authorization/password-authorized-client-provider.d.ts.map +1 -0
  74. package/lib/node/authorization/password-authorized-client-provider.js +85 -0
  75. package/lib/node/authorization/password-authorized-client-provider.js.map +1 -0
  76. package/lib/node/authorization/refresh-token-authorized-client-provider.d.ts +12 -0
  77. package/lib/node/authorization/refresh-token-authorized-client-provider.d.ts.map +1 -0
  78. package/lib/node/authorization/refresh-token-authorized-client-provider.js +68 -0
  79. package/lib/node/authorization/refresh-token-authorized-client-provider.js.map +1 -0
  80. package/lib/node/constants.d.ts +5 -0
  81. package/lib/node/constants.d.ts.map +1 -0
  82. package/lib/node/constants.js +8 -0
  83. package/lib/node/constants.js.map +1 -0
  84. package/lib/node/endpoint/authorization-code-token-response-client.d.ts +12 -0
  85. package/lib/node/endpoint/authorization-code-token-response-client.d.ts.map +1 -0
  86. package/lib/node/endpoint/authorization-code-token-response-client.js +83 -0
  87. package/lib/node/endpoint/authorization-code-token-response-client.js.map +1 -0
  88. package/lib/node/endpoint/client-credentials-token-response-client.d.ts +12 -0
  89. package/lib/node/endpoint/client-credentials-token-response-client.d.ts.map +1 -0
  90. package/lib/node/endpoint/client-credentials-token-response-client.js +80 -0
  91. package/lib/node/endpoint/client-credentials-token-response-client.js.map +1 -0
  92. package/lib/node/endpoint/endpoint-protocol.d.ts +48 -0
  93. package/lib/node/endpoint/endpoint-protocol.d.ts.map +1 -0
  94. package/lib/node/endpoint/endpoint-protocol.js +9 -0
  95. package/lib/node/endpoint/endpoint-protocol.js.map +1 -0
  96. package/lib/node/endpoint/index.d.ts +7 -0
  97. package/lib/node/endpoint/index.d.ts.map +1 -0
  98. package/lib/node/endpoint/index.js +23 -0
  99. package/lib/node/endpoint/index.js.map +1 -0
  100. package/lib/node/endpoint/password-token-response-client.d.ts +12 -0
  101. package/lib/node/endpoint/password-token-response-client.d.ts.map +1 -0
  102. package/lib/node/endpoint/password-token-response-client.js +82 -0
  103. package/lib/node/endpoint/password-token-response-client.js.map +1 -0
  104. package/lib/node/endpoint/refresh-token-token-response-client.d.ts +12 -0
  105. package/lib/node/endpoint/refresh-token-token-response-client.d.ts.map +1 -0
  106. package/lib/node/endpoint/refresh-token-token-response-client.js +87 -0
  107. package/lib/node/endpoint/refresh-token-token-response-client.js.map +1 -0
  108. package/lib/node/endpoint/user-response-client.d.ts +9 -0
  109. package/lib/node/endpoint/user-response-client.d.ts.map +1 -0
  110. package/lib/node/endpoint/user-response-client.js +84 -0
  111. package/lib/node/endpoint/user-response-client.js.map +1 -0
  112. package/lib/node/error/error-hander.d.ts +11 -0
  113. package/lib/node/error/error-hander.d.ts.map +1 -0
  114. package/lib/node/error/error-hander.js +52 -0
  115. package/lib/node/error/error-hander.js.map +1 -0
  116. package/lib/node/error/error-protocol.d.ts +3 -0
  117. package/lib/node/error/error-protocol.d.ts.map +1 -0
  118. package/lib/node/error/error-protocol.js +7 -0
  119. package/lib/node/error/error-protocol.js.map +1 -0
  120. package/lib/node/error/error.d.ts +10 -0
  121. package/lib/node/error/error.d.ts.map +1 -0
  122. package/lib/node/error/error.js +23 -0
  123. package/lib/node/error/error.js.map +1 -0
  124. package/lib/node/error/index.d.ts +4 -0
  125. package/lib/node/error/index.d.ts.map +1 -0
  126. package/lib/node/error/index.js +20 -0
  127. package/lib/node/error/index.js.map +1 -0
  128. package/lib/node/index.d.ts +8 -0
  129. package/lib/node/index.d.ts.map +1 -0
  130. package/lib/node/index.js +24 -0
  131. package/lib/node/index.js.map +1 -0
  132. package/lib/node/module.d.ts +4 -0
  133. package/lib/node/module.d.ts.map +1 -0
  134. package/lib/node/module.js +6 -0
  135. package/lib/node/module.js.map +1 -0
  136. package/lib/node/provider/index.d.ts +3 -0
  137. package/lib/node/provider/index.d.ts.map +1 -0
  138. package/lib/node/provider/index.js +19 -0
  139. package/lib/node/provider/index.js.map +1 -0
  140. package/lib/node/provider/provider-details-manager.d.ts +12 -0
  141. package/lib/node/provider/provider-details-manager.d.ts.map +1 -0
  142. package/lib/node/provider/provider-details-manager.js +50 -0
  143. package/lib/node/provider/provider-details-manager.js.map +1 -0
  144. package/lib/node/provider/provider-protocol.d.ts +21 -0
  145. package/lib/node/provider/provider-protocol.d.ts.map +1 -0
  146. package/lib/node/provider/provider-protocol.js +5 -0
  147. package/lib/node/provider/provider-protocol.js.map +1 -0
  148. package/lib/node/registration/id-token-jwt-decoder-facatory.d.ts +15 -0
  149. package/lib/node/registration/id-token-jwt-decoder-facatory.d.ts.map +1 -0
  150. package/lib/node/registration/id-token-jwt-decoder-facatory.js +89 -0
  151. package/lib/node/registration/id-token-jwt-decoder-facatory.js.map +1 -0
  152. package/lib/node/registration/index.d.ts +5 -0
  153. package/lib/node/registration/index.d.ts.map +1 -0
  154. package/lib/node/registration/index.js +21 -0
  155. package/lib/node/registration/index.js.map +1 -0
  156. package/lib/node/registration/jws-algorithm-resolver.d.ts +5 -0
  157. package/lib/node/registration/jws-algorithm-resolver.d.ts.map +1 -0
  158. package/lib/node/registration/jws-algorithm-resolver.js +22 -0
  159. package/lib/node/registration/jws-algorithm-resolver.js.map +1 -0
  160. package/lib/node/registration/registration-manager.d.ts +15 -0
  161. package/lib/node/registration/registration-manager.d.ts.map +1 -0
  162. package/lib/node/registration/registration-manager.js +64 -0
  163. package/lib/node/registration/registration-manager.js.map +1 -0
  164. package/lib/node/registration/registration-protocol.d.ts +22 -0
  165. package/lib/node/registration/registration-protocol.d.ts.map +1 -0
  166. package/lib/node/registration/registration-protocol.js +7 -0
  167. package/lib/node/registration/registration-protocol.js.map +1 -0
  168. package/lib/node/user/index.d.ts +4 -0
  169. package/lib/node/user/index.d.ts.map +1 -0
  170. package/lib/node/user/index.js +20 -0
  171. package/lib/node/user/index.js.map +1 -0
  172. package/lib/node/user/oauth2-user-service.d.ts +10 -0
  173. package/lib/node/user/oauth2-user-service.d.ts.map +1 -0
  174. package/lib/node/user/oauth2-user-service.js +61 -0
  175. package/lib/node/user/oauth2-user-service.js.map +1 -0
  176. package/lib/node/user/oidc-user-service.d.ts +13 -0
  177. package/lib/node/user/oidc-user-service.d.ts.map +1 -0
  178. package/lib/node/user/oidc-user-service.js +98 -0
  179. package/lib/node/user/oidc-user-service.js.map +1 -0
  180. package/lib/node/user/user-protocol.d.ts +6 -0
  181. package/lib/node/user/user-protocol.d.ts.map +1 -0
  182. package/lib/node/user/user-protocol.js +3 -0
  183. package/lib/node/user/user-protocol.js.map +1 -0
  184. package/lib/node/utils/authorization-grant-request-entity-util.d.ts +10 -0
  185. package/lib/node/utils/authorization-grant-request-entity-util.d.ts.map +1 -0
  186. package/lib/node/utils/authorization-grant-request-entity-util.js +28 -0
  187. package/lib/node/utils/authorization-grant-request-entity-util.js.map +1 -0
  188. package/lib/node/utils/authorization-response-util.d.ts +8 -0
  189. package/lib/node/utils/authorization-response-util.d.ts.map +1 -0
  190. package/lib/node/utils/authorization-response-util.js +53 -0
  191. package/lib/node/utils/authorization-response-util.js.map +1 -0
  192. package/lib/node/utils/index.d.ts +3 -0
  193. package/lib/node/utils/index.d.ts.map +1 -0
  194. package/lib/node/utils/index.js +19 -0
  195. package/lib/node/utils/index.js.map +1 -0
  196. package/lib/package.spec.d.ts +1 -0
  197. package/lib/package.spec.d.ts.map +1 -0
  198. package/lib/package.spec.js +4 -0
  199. package/lib/package.spec.js.map +1 -0
  200. package/package.json +51 -0
  201. package/src/node/authentication/authentication-protocol.ts +26 -0
  202. package/src/node/authentication/index.ts +3 -0
  203. package/src/node/authentication/oauth2-authentication-provider.ts +163 -0
  204. package/src/node/authentication/oidc-authentication-provider.ts +60 -0
  205. package/src/node/authorization/authorization-code-authorized-client-provider.ts +16 -0
  206. package/src/node/authorization/authorization-code-grant-middleware.ts +110 -0
  207. package/src/node/authorization/authorization-failure-handler.ts +23 -0
  208. package/src/node/authorization/authorization-protocol.ts +104 -0
  209. package/src/node/authorization/authorization-request-manager.ts +54 -0
  210. package/src/node/authorization/authorization-request-redirect-middleware.ts +91 -0
  211. package/src/node/authorization/authorization-request-resolver.ts +149 -0
  212. package/src/node/authorization/authorization-success-handler.ts +15 -0
  213. package/src/node/authorization/authorized-client-manager.ts +35 -0
  214. package/src/node/authorization/authorized-client-service.ts +73 -0
  215. package/src/node/authorization/client-credentials-authorized-client-provider.ts +61 -0
  216. package/src/node/authorization/context-attributes-mapper.ts +18 -0
  217. package/src/node/authorization/index.ts +13 -0
  218. package/src/node/authorization/password-authorized-client-provider.ts +78 -0
  219. package/src/node/authorization/refresh-token-authorized-client-provider.ts +58 -0
  220. package/src/node/constants.ts +4 -0
  221. package/src/node/endpoint/authorization-code-token-response-client.ts +73 -0
  222. package/src/node/endpoint/client-credentials-token-response-client.ts +69 -0
  223. package/src/node/endpoint/endpoint-protocol.ts +55 -0
  224. package/src/node/endpoint/index.ts +6 -0
  225. package/src/node/endpoint/password-token-response-client.ts +71 -0
  226. package/src/node/endpoint/refresh-token-token-response-client.ts +77 -0
  227. package/src/node/endpoint/user-response-client.ts +72 -0
  228. package/src/node/error/error-hander.ts +39 -0
  229. package/src/node/error/error-protocol.ts +6 -0
  230. package/src/node/error/error.ts +19 -0
  231. package/src/node/error/index.ts +3 -0
  232. package/src/node/index.ts +7 -0
  233. package/src/node/module.ts +4 -0
  234. package/src/node/provider/index.ts +2 -0
  235. package/src/node/provider/provider-details-manager.ts +33 -0
  236. package/src/node/provider/provider-protocol.ts +24 -0
  237. package/src/node/registration/id-token-jwt-decoder-facatory.ts +71 -0
  238. package/src/node/registration/index.ts +4 -0
  239. package/src/node/registration/jws-algorithm-resolver.ts +12 -0
  240. package/src/node/registration/registration-manager.ts +47 -0
  241. package/src/node/registration/registration-protocol.ts +27 -0
  242. package/src/node/user/index.ts +3 -0
  243. package/src/node/user/oauth2-user-service.ts +50 -0
  244. package/src/node/user/oidc-user-service.ts +92 -0
  245. package/src/node/user/user-protocol.ts +6 -0
  246. package/src/node/utils/authorization-grant-request-entity-util.ts +26 -0
  247. package/src/node/utils/authorization-response-util.ts +50 -0
  248. package/src/node/utils/index.ts +2 -0
  249. package/src/package.spec.ts +4 -0
@@ -0,0 +1,39 @@
1
+ import { OAUTH2_AUTHORIZATION_ERROR_HANDLER_PRIORITY } from './error-protocol';
2
+ import { ErrorHandler, Context, RedirectStrategy } from '@celljs/web/lib/node';
3
+ import { Component, Autowired } from '@celljs/core';
4
+ import { OAuth2AuthorizationError, AuthorizationRequest, OAuth2ParameterNames } from '@celljs/oauth2-core';
5
+ import { AuthorizationRequestManager } from '../authorization';
6
+ import { enc } from 'crypto-js';
7
+
8
+ @Component(ErrorHandler)
9
+ export class OAuth2AuthorizationErrorHandler implements ErrorHandler {
10
+ readonly priority: number = OAUTH2_AUTHORIZATION_ERROR_HANDLER_PRIORITY;
11
+
12
+ @Autowired(RedirectStrategy)
13
+ protected readonly redirectStrategy: RedirectStrategy;
14
+
15
+ @Autowired(AuthorizationRequestManager)
16
+ protected readonly authorizationRequestManager: AuthorizationRequestManager<AuthorizationRequest>;
17
+
18
+ canHandle(ctx: Context, err: Error): Promise<boolean> {
19
+ return Promise.resolve(err instanceof OAuth2AuthorizationError);
20
+ }
21
+
22
+ async handle(ctx: Context, err: OAuth2AuthorizationError): Promise<void> {
23
+ const { oauth2Error } = err;
24
+ const authorizationRequest = <AuthorizationRequest>await this.authorizationRequestManager.remove();
25
+
26
+ let redirectUri = `${authorizationRequest.redirectUri}?${OAuth2ParameterNames.ERROR}=${oauth2Error.errorCode}`;
27
+ if (oauth2Error.description) {
28
+ redirectUri = `${redirectUri}&${OAuth2ParameterNames.ERROR_DESCRIPTION}=${enc.Base64.stringify(enc.Utf8.parse(oauth2Error.description))}`;
29
+ }
30
+
31
+ if (oauth2Error.uri) {
32
+ redirectUri = `${redirectUri}&${OAuth2ParameterNames.ERROR_URI}=${enc.Base64.stringify(enc.Utf8.parse(oauth2Error.uri))}`;
33
+
34
+ }
35
+
36
+ await this.redirectStrategy.send(redirectUri);
37
+ ctx.response.end(err.message);
38
+ }
39
+ }
@@ -0,0 +1,6 @@
1
+ import { HTTP_ERROR_HANDLER_PRIORITY } from '@celljs/web/lib/node';
2
+
3
+ export const OAUTH2_AUTHORIZATION_ERROR_HANDLER_PRIORITY = HTTP_ERROR_HANDLER_PRIORITY + 100;
4
+
5
+ export const CLIENT_AUTHORIZATION_REQUIRED_ERROR_CODE = 'client_authorization_required';
6
+
@@ -0,0 +1,19 @@
1
+ import { OAuth2AuthorizationError, OAuth2Error } from '@celljs/oauth2-core';
2
+ import { CLIENT_AUTHORIZATION_REQUIRED_ERROR_CODE } from './error-protocol';
3
+
4
+ export class ClientAuthorizationError extends OAuth2AuthorizationError {
5
+
6
+ constructor(public override oauth2Error: OAuth2Error, public clientRegistrationId: string, error?: Error) {
7
+ super(oauth2Error, error);
8
+ }
9
+
10
+ }
11
+
12
+ export class ClientAuthorizationRequiredError extends ClientAuthorizationError {
13
+ constructor(clientRegistrationId: string) {
14
+ super({
15
+ errorCode: CLIENT_AUTHORIZATION_REQUIRED_ERROR_CODE,
16
+ description: `Authorization required for Client Registration Id: ${clientRegistrationId}`
17
+ }, clientRegistrationId);
18
+ }
19
+ }
@@ -0,0 +1,3 @@
1
+ export * from './error-hander';
2
+ export * from './error-protocol';
3
+ export * from './error';
@@ -0,0 +1,7 @@
1
+ export * from './registration';
2
+ export * from './provider';
3
+ export * from './authorization';
4
+ export * from './authentication';
5
+ export * from './endpoint';
6
+ export * from './constants';
7
+ export * from './user';
@@ -0,0 +1,4 @@
1
+ import { autoBind } from '@celljs/core';
2
+ import './index';
3
+
4
+ export default autoBind();
@@ -0,0 +1,2 @@
1
+ export * from './provider-protocol';
2
+ export * from './provider-details-manager';
@@ -0,0 +1,33 @@
1
+ import { ProviderDetailsManager, ProviderDetails } from './provider-protocol';
2
+ import { Value, Component } from '@celljs/core';
3
+
4
+ @Component(ProviderDetailsManager)
5
+ export class InMemoryProviderDetailsManager implements ProviderDetailsManager {
6
+
7
+ @Value('cell.oauth2.client.providers')
8
+ protected providers: { [id: string]: ProviderDetails } = {};
9
+
10
+ @Value('cell.oauth2.client.providerTemplates')
11
+ protected readonly providerTemplates: { [id: string]: ProviderDetails } = {};
12
+
13
+ protected initialized = false;
14
+
15
+ async get(providerId: string): Promise<ProviderDetails | undefined> {
16
+ if (!this.initialized) {
17
+ for (const id in this.providers) {
18
+ if (Object.prototype.hasOwnProperty.call(this.providers, id)) {
19
+ const template = this.providerTemplates[id];
20
+ const provider = { ...template, ...this.providers[id] };
21
+ provider.userInfoEndpoint = { ...template?.userInfoEndpoint, ...provider.userInfoEndpoint };
22
+ provider.configurationMetadata = { ...template?.configurationMetadata, ...provider.configurationMetadata };
23
+ this.providers[id] = provider;
24
+ }
25
+ }
26
+ this.providers = this.providers || {};
27
+ this.initialized = true;
28
+ }
29
+
30
+ return this.providers[providerId] || this.providerTemplates[providerId];
31
+ }
32
+
33
+ }
@@ -0,0 +1,24 @@
1
+ import { AuthenticationMethod } from '@celljs/oauth2-core';
2
+
3
+ export const ProviderDetailsManager = Symbol('ProviderDetailsManager');
4
+
5
+ export interface ProviderDetails {
6
+ authorizationUri: string;
7
+ tokenUri: string;
8
+ userInfoEndpoint: UserInfoEndpoint;
9
+ jwkSetUri: string;
10
+ issuerUri: string;
11
+ configurationMetadata: { [key: string]: any };
12
+ }
13
+
14
+ export interface UserInfoEndpoint {
15
+ uri: string;
16
+ authenticationMethod: AuthenticationMethod;
17
+ userNameAttributeName: string;
18
+ }
19
+
20
+ export interface ProviderDetailsManager {
21
+
22
+ get(providerId: string): Promise<ProviderDetails | undefined>;
23
+
24
+ }
@@ -0,0 +1,71 @@
1
+ import { JwtDecoderFactory, JwtDecoder, JwkSetManager, JwsAlgorithms } from '@celljs/oauth2-jose';
2
+ import { ClientRegistration, JwsAlgorithmResolver, MISSING_SIGNATURE_VERIFIER_ERROR_CODE } from './registration-protocol';
3
+ import { Component, Autowired } from '@celljs/core';
4
+ import { ProviderDetailsManager, ProviderDetails } from '../provider';
5
+ import { jwtVerify, KeyLike, generateSecret } from 'jose';
6
+ import { OAuth2AuthenticationError, AuthorizationRequest } from '@celljs/oauth2-core';
7
+ import { AuthorizationRequestManager } from '../authorization';
8
+
9
+ @Component()
10
+ export class IdTokenJwtDecoderFactory implements JwtDecoderFactory<ClientRegistration> {
11
+
12
+ @Autowired(JwsAlgorithmResolver)
13
+ protected readonly jwsAlgorithmResolver: JwsAlgorithmResolver;
14
+
15
+ @Autowired(AuthorizationRequestManager)
16
+ protected readonly authorizationRequestManager: AuthorizationRequestManager<AuthorizationRequest>;
17
+
18
+ @Autowired(JwkSetManager)
19
+ protected readonly jwkSetManager: JwkSetManager<KeyLike | Uint8Array>;
20
+
21
+ @Autowired(ProviderDetailsManager)
22
+ protected readonly providerDetailsManager: ProviderDetailsManager;
23
+
24
+ protected readonly jwtDecoders = new Map<string, JwtDecoder>();
25
+
26
+ async create(clientRegistration: ClientRegistration): Promise<JwtDecoder> {
27
+ const { registrationId } = clientRegistration;
28
+ let jwtDecoder = this.jwtDecoders.get(registrationId);
29
+ if (!jwtDecoder) {
30
+ const jwsAlgorithm = await this.jwsAlgorithmResolver.reolve(clientRegistration);
31
+ const { jwkSetUri, issuerUri } = <ProviderDetails> await this.providerDetailsManager.get(registrationId);
32
+
33
+ let key: KeyLike | Uint8Array;
34
+ if (jwsAlgorithm === JwsAlgorithms.RS256) {
35
+ if (!jwkSetUri) {
36
+ throw new OAuth2AuthenticationError({
37
+ errorCode: MISSING_SIGNATURE_VERIFIER_ERROR_CODE,
38
+ description: `Failed to find a Signature Verifier for Client Registration: '${registrationId}'. Check to ensure you have configured the JwkSet URI.`
39
+ });
40
+ }
41
+ key = await this.jwkSetManager.get(jwkSetUri);
42
+ } else {
43
+ const { clientSecret } = clientRegistration;
44
+ if (!clientSecret) {
45
+ throw new OAuth2AuthenticationError({
46
+ errorCode: MISSING_SIGNATURE_VERIFIER_ERROR_CODE,
47
+ description: `Failed to find a Signature Verifier for Client Registration: '${registrationId}'. Check to ensure you have configured the client secret.`
48
+ });
49
+ }
50
+ key = await generateSecret(clientRegistration.clientSecret);
51
+ }
52
+ jwtDecoder = {
53
+ decode: async token => {
54
+ const { payload, protectedHeader } = await jwtVerify(token, key, {
55
+ issuer: issuerUri,
56
+ audience: clientRegistration.clientId,
57
+ algorithms: [ jwsAlgorithm ]
58
+ });
59
+ return {
60
+ header: protectedHeader,
61
+ payload,
62
+ token
63
+ };
64
+ }
65
+ };
66
+ this.jwtDecoders.set(registrationId, jwtDecoder);
67
+ }
68
+ return jwtDecoder;
69
+ }
70
+
71
+ }
@@ -0,0 +1,4 @@
1
+ export * from './registration-protocol';
2
+ export * from './registration-manager';
3
+ export * from './id-token-jwt-decoder-facatory';
4
+ export * from './jws-algorithm-resolver';
@@ -0,0 +1,12 @@
1
+ import { JwsAlgorithms } from '@celljs/oauth2-jose';
2
+ import { ClientRegistration, JwsAlgorithmResolver } from './registration-protocol';
3
+ import { Component } from '@celljs/core';
4
+
5
+ @Component(JwsAlgorithmResolver)
6
+ export class DefaultJwsAlgorithmResolver implements JwsAlgorithmResolver {
7
+
8
+ async reolve(clientRegistration: ClientRegistration): Promise<string> {
9
+ return JwsAlgorithms.RS256;
10
+ }
11
+
12
+ }
@@ -0,0 +1,47 @@
1
+ import { ClientRegistrationManager, ClientRegistration } from './registration-protocol';
2
+ import { Value, Component } from '@celljs/core';
3
+ import { AuthorizationGrantType, ClientAuthenticationMethod } from '@celljs/oauth2-core';
4
+
5
+ @Component(ClientRegistrationManager)
6
+ export class InMemoryClientRegistrationManager implements ClientRegistrationManager {
7
+
8
+ @Value('cell.oauth2.client.registrations')
9
+ protected readonly registrations: { [id: string]: ClientRegistration } = {};
10
+
11
+ @Value('cell.oauth2.client.registrationTemplates')
12
+ protected readonly registrationTemplates: { [id: string]: ClientRegistration } = {};
13
+
14
+ @Value('cell.oauth2.client.defaultRedirectUri')
15
+ protected readonly defaultRedirectUri: string;
16
+
17
+ protected initialized = false;
18
+
19
+ async get(registrationId: string): Promise<ClientRegistration | undefined> {
20
+ if (!this.initialized) {
21
+ for (const id in this.registrations) {
22
+ if (Object.prototype.hasOwnProperty.call(this.registrations, id)) {
23
+ const template = this.registrationTemplates[id];
24
+ const registration = { ...template, ...this.registrations[id] };
25
+ this.registrations[id] = registration;
26
+ registration.registrationId = id;
27
+ registration.clientAuthenticationMethod = registration.clientAuthenticationMethod || this.deduceClientAuthenticationMethod(registration);
28
+ registration.redirectUri = registration.redirectUri || this.defaultRedirectUri;
29
+ registration.provider = registration.provider || id;
30
+ registration.clientName = registration.clientName || id;
31
+ }
32
+ }
33
+ this.initialized = true;
34
+ }
35
+
36
+ return this.registrations[registrationId];
37
+ }
38
+
39
+ protected deduceClientAuthenticationMethod(clientRegistration: ClientRegistration) {
40
+ if (AuthorizationGrantType.AuthorizationCode === clientRegistration.authorizationGrantType
41
+ && !clientRegistration.clientSecret) {
42
+ return ClientAuthenticationMethod.None;
43
+ }
44
+ return ClientAuthenticationMethod.Basic;
45
+ }
46
+
47
+ }
@@ -0,0 +1,27 @@
1
+ import { ClientAuthenticationMethod, AuthorizationGrantType } from '@celljs/oauth2-core';
2
+
3
+ export const ClientRegistrationManager = Symbol('ClientRegistrationManager');
4
+ export const JwsAlgorithmResolver = Symbol('JwsAlgorithmResolver');
5
+ export const MISSING_SIGNATURE_VERIFIER_ERROR_CODE = 'missing_signature_verifier';
6
+
7
+ export interface ClientRegistration {
8
+ registrationId: string;
9
+ clientId: string;
10
+ clientSecret: string;
11
+ clientAuthenticationMethod: ClientAuthenticationMethod;
12
+ authorizationGrantType: AuthorizationGrantType;
13
+ redirectUri: string;
14
+ scopes: string[];
15
+ provider: string;
16
+ clientName: string;
17
+ }
18
+
19
+ export interface ClientRegistrationManager {
20
+
21
+ get(registrationId: string): Promise<ClientRegistration | undefined>;
22
+
23
+ }
24
+
25
+ export interface JwsAlgorithmResolver {
26
+ reolve(clientRegistration: ClientRegistration): Promise<string>;
27
+ }
@@ -0,0 +1,3 @@
1
+ export * from './user-protocol';
2
+ export * from './oauth2-user-service';
3
+ export * from './oidc-user-service';
@@ -0,0 +1,50 @@
1
+ import { Component, Autowired } from '@celljs/core';
2
+ import { UserService } from '@celljs/security/lib/node';
3
+ import { User } from '@celljs/security';
4
+ import { OAuth2UserRequest, UserResponseClient, MISSING_USER_INFO_URI_ERROR_CODE, MISSING_USER_NAME_ATTRIBUTE_ERROR_CODE } from '../endpoint';
5
+ import { ProviderDetailsManager } from '../provider';
6
+ import { OAuth2AuthenticationError } from '@celljs/oauth2-core';
7
+
8
+ @Component()
9
+ export class OAuth2UserService implements UserService<OAuth2UserRequest, User> {
10
+
11
+ @Autowired(ProviderDetailsManager)
12
+ protected readonly providerDetailsManager: ProviderDetailsManager;
13
+
14
+ @Autowired(UserResponseClient)
15
+ protected readonly userResponseClient: UserResponseClient<OAuth2UserRequest>;
16
+
17
+ async load(userRequest: OAuth2UserRequest): Promise<User> {
18
+ const { clientRegistration } = userRequest;
19
+ const providerDetails = await this.providerDetailsManager.get(clientRegistration.provider);
20
+ if (!providerDetails?.userInfoEndpoint.uri) {
21
+ throw new OAuth2AuthenticationError({
22
+ errorCode: MISSING_USER_INFO_URI_ERROR_CODE,
23
+ description: `Missing required UserInfo Uri in UserInfoEndpoint for Client Registration: ${clientRegistration.registrationId}`
24
+ });
25
+ }
26
+ const userNameAttributeName = providerDetails.userInfoEndpoint.userNameAttributeName;
27
+ if (!userNameAttributeName) {
28
+ throw new OAuth2AuthenticationError({
29
+ errorCode: MISSING_USER_NAME_ATTRIBUTE_ERROR_CODE,
30
+ description: `Missing required "user name" attribute name in UserInfoEndpoint for Client Registration: ${clientRegistration.registrationId}`
31
+ });
32
+ }
33
+ const { userAttributes } = await this.userResponseClient.getUserResponse(userRequest);
34
+
35
+ const claims = userAttributes;
36
+
37
+ return {
38
+ type: clientRegistration.registrationId,
39
+ username: claims[userNameAttributeName],
40
+ password: '',
41
+ accountNonExpired: true,
42
+ accountNonLocked: true,
43
+ credentialsNonExpired: true,
44
+ enabled: true,
45
+ policies: [],
46
+ claims
47
+ };
48
+ }
49
+ }
50
+
@@ -0,0 +1,92 @@
1
+ import { Value, Component, Autowired } from '@celljs/core';
2
+ import { UserService } from '@celljs/security/lib/node';
3
+ import { User } from '@celljs/security';
4
+ import { OidcUserRequest } from './user-protocol';
5
+ import { OAuth2UserRequest, UserResponseClient, INVALID_USER_INFO_RESPONSE_ERROR_CODE } from '../endpoint';
6
+ import { ProviderDetailsManager } from '../provider';
7
+ import { AuthorizationGrantType, StandardClaimNames, OAuth2AuthenticationError, IdTokenClaimNames } from '@celljs/oauth2-core';
8
+
9
+ @Component()
10
+ export class OidcUserService implements UserService<OidcUserRequest, User> {
11
+
12
+ @Value('cell.oauth2.client.accessibleScopes')
13
+ protected readonly accessibleScopes: string[];
14
+
15
+ @Autowired(ProviderDetailsManager)
16
+ protected readonly providerDetailsManager: ProviderDetailsManager;
17
+
18
+ @Autowired(UserResponseClient)
19
+ protected readonly userResponseClient: UserResponseClient<OAuth2UserRequest>;
20
+
21
+ async load(userRequest: OidcUserRequest): Promise<User> {
22
+ const { idToken, clientRegistration } = userRequest;
23
+ let claims: { [key: string]: any } = {};
24
+ if (await this.shuldRetrieveuserInfo(userRequest)) {
25
+ const { userAttributes } = await this.userResponseClient.getUserResponse(userRequest);
26
+ claims = { ...userAttributes };
27
+ const subject = claims[StandardClaimNames.SUB];
28
+
29
+ // https://openid.net/specs/openid-connect-core-1_0.html#UserInfoResponse
30
+
31
+ // 1) The sub (subject) Claim MUST always be returned in the UserInfo Response
32
+ if (!subject) {
33
+ throw new OAuth2AuthenticationError({
34
+ errorCode: INVALID_USER_INFO_RESPONSE_ERROR_CODE
35
+ });
36
+ }
37
+
38
+ // 2) Due to the possibility of token substitution attacks (see Section 16.11),
39
+ // the UserInfo Response is not guaranteed to be about the End-User
40
+ // identified by the sub (subject) element of the ID Token.
41
+ // The sub Claim in the UserInfo Response MUST be verified to exactly match
42
+ // the sub Claim in the ID Token; if they do not match,
43
+ // the UserInfo Response values MUST NOT be used.
44
+ if (subject !== idToken.payload[IdTokenClaimNames.SUB]) {
45
+ throw new OAuth2AuthenticationError({
46
+ errorCode: INVALID_USER_INFO_RESPONSE_ERROR_CODE
47
+ });
48
+ }
49
+
50
+ }
51
+
52
+ claims = { ...claims, ...idToken.payload };
53
+ const providerDetails = await this.providerDetailsManager.get(clientRegistration.provider);
54
+ const userNameAttributeName = providerDetails?.userInfoEndpoint?.userNameAttributeName || IdTokenClaimNames.SUB;
55
+
56
+ return {
57
+ type: clientRegistration.registrationId,
58
+ username: claims[userNameAttributeName],
59
+ password: '',
60
+ accountNonExpired: true,
61
+ accountNonLocked: true,
62
+ credentialsNonExpired: true,
63
+ enabled: true,
64
+ policies: [],
65
+ claims,
66
+ idToken
67
+ };
68
+ }
69
+
70
+ protected async shuldRetrieveuserInfo(userRequest: OidcUserRequest) {
71
+ const { clientRegistration, accessToken } = userRequest;
72
+ const providerDetails = await this.providerDetailsManager.get(clientRegistration.provider);
73
+
74
+ // Auto-disabled if UserInfo Endpoint URI is not provided
75
+ if (!providerDetails?.userInfoEndpoint?.uri) {
76
+ return false;
77
+ }
78
+
79
+ // The Claims requested by the profile, email, address, and phone scope values
80
+ // are returned from the UserInfo Endpoint (as described in Section 5.3.2),
81
+ // when a response_type value is used that results in an Access Token being issued.
82
+ // However, when no Access Token is issued, which is the case for the response_type=id_token,
83
+ // the resulting Claims are returned in the ID Token.
84
+ // The Authorization Code Grant Flow, which is response_type=code, results in an Access Token being issued.
85
+ if (AuthorizationGrantType.AuthorizationCode === clientRegistration.authorizationGrantType) {
86
+
87
+ // Return true if there is at least one match between the authorized scope(s) and accessible scope(s)
88
+ return !this.accessibleScopes?.length || this.accessibleScopes.some(v => accessToken.scopes.indexOf(v) !== -1);
89
+ }
90
+ return false;
91
+ }
92
+ }
@@ -0,0 +1,6 @@
1
+ import { Jwt } from '@celljs/oauth2-jose';
2
+ import { OAuth2UserRequest } from '../endpoint';
3
+
4
+ export interface OidcUserRequest extends OAuth2UserRequest {
5
+ idToken: Jwt;
6
+ }
@@ -0,0 +1,26 @@
1
+ import { ClientAuthenticationMethod } from '@celljs/oauth2-core';
2
+ import { ClientRegistration } from '../registration';
3
+ import { MediaType, HttpHeaders } from '@celljs/http';
4
+ import { enc } from 'crypto-js';
5
+
6
+ export namespace AuthorizationGrantRequestEntityUtil {
7
+
8
+ export function getTokenRequestHeaders(clientRegistration: ClientRegistration): { [key: string]: any } {
9
+ const headers = { ...getDefaultTokenRequestHeaders() };
10
+ const { clientAuthenticationMethod, clientId, clientSecret } = clientRegistration;
11
+ if (clientAuthenticationMethod === ClientAuthenticationMethod.Basic) {
12
+ const credentialsStr = `${clientId}:${clientSecret}`;
13
+ const encoded = enc.Latin1.parse(credentialsStr);
14
+ headers[HttpHeaders.AUTHORIZATION] = `${clientAuthenticationMethod} ${enc.Base64.stringify(encoded)}`;
15
+ }
16
+ return headers;
17
+ }
18
+
19
+ export function getDefaultTokenRequestHeaders(): { [key: string]: any } {
20
+
21
+ return {
22
+ [HttpHeaders.ACCEPT]: MediaType.APPLICATION_JSON_UTF8,
23
+ [HttpHeaders.CONTENT_TYPE]: `${MediaType.APPLICATION_FORM_URLENCODED};charset=UTF-8`
24
+ };
25
+ }
26
+ }
@@ -0,0 +1,50 @@
1
+ import { Context } from '@celljs/web/lib/node';
2
+ import { OAuth2ParameterNames, AuthorizationResponse } from '@celljs/oauth2-core';
3
+
4
+ export namespace AuthorizationResponseUtil {
5
+
6
+ export function isAuthorizationResponse(): boolean {
7
+ return isAuthorizationResponseSuccess() || isAuthorizationResponseError();
8
+ }
9
+
10
+ export function isAuthorizationResponseSuccess(): boolean {
11
+ const request = Context.getRequest();
12
+ const query = request.query || {};
13
+ return !!(query[OAuth2ParameterNames.CODE] && query[OAuth2ParameterNames.STATE]);
14
+ }
15
+
16
+ export function isAuthorizationResponseError(): boolean {
17
+ const request = Context.getRequest();
18
+ const query = request.query || {};
19
+ return !!(query[OAuth2ParameterNames.ERROR] && query[OAuth2ParameterNames.STATE]);
20
+ }
21
+
22
+ export function convert(redirectUri: string) {
23
+ const request = Context.getRequest();
24
+ const query = request.query || {};
25
+
26
+ const code = query[OAuth2ParameterNames.CODE];
27
+ const errorCode = query[OAuth2ParameterNames.ERROR];
28
+ const state = query[OAuth2ParameterNames.STATE];
29
+
30
+ if (code) {
31
+ return <AuthorizationResponse>{
32
+ code,
33
+ redirectUri,
34
+ state
35
+ };
36
+ } else {
37
+ const errorDescription = query[OAuth2ParameterNames.ERROR_DESCRIPTION];
38
+ const errorUri = query[OAuth2ParameterNames.ERROR_URI];
39
+ return <AuthorizationResponse>{
40
+ redirectUri,
41
+ state,
42
+ error: {
43
+ description: errorDescription,
44
+ uri: errorUri,
45
+ errorCode
46
+ },
47
+ };
48
+ }
49
+ }
50
+ }
@@ -0,0 +1,2 @@
1
+ export * from './authorization-response-util';
2
+ export * from './authorization-grant-request-entity-util';
@@ -0,0 +1,4 @@
1
+ describe('oauth2 client package', () => {
2
+
3
+ it('support code coverage statistics', () => {});
4
+ });