import { OAuthException } from "./exceptions/oauth.exception" ; import { GrantIdentifier , GrantInterface } from "./grants/abstract/grant.interface" ; import { AuthCodeGrant } from "./grants/auth_code.grant" ; import { ClientCredentialsGrant } from "./grants/client_credentials.grant" ; import { ImplicitGrant } from "./grants/implicit.grant" ; import { PasswordGrant } from "./grants/password.grant" ; import { RefreshTokenGrant } from "./grants/refresh_token.grant" ; import { OAuthTokenRepository } from "./repositories/access_token.repository" ; import { OAuthAuthCodeRepository } from "./repositories/auth_code.repository" ; import { OAuthClientRepository } from "./repositories/client.repository" ; import { OAuthScopeRepository } from "./repositories/scope.repository" ; import { OAuthUserRepository } from "./repositories/user.repository" ; import { AuthorizationRequest } from "./requests/authorization.request" ; import { RequestInterface } from "./requests/request" ; import { ResponseInterface } from "./responses/response" ; import { DateInterval } from "./utils/date_interval" ; import { JwtInterface } from "./utils/jwt" ; export interface AuthorizationServerOptions { notBeforeLeeway : number ; requiresPKCE : boolean ; tokenCID : "id" | "name" ; } type EnableGrantTuple = GrantIdentifier | [ GrantIdentifier , DateInterval ] ; export class AuthorizationServer { private readonly enabledGrantTypes : { [ key ] : GrantInterface ; } = { } ; private readonly grantTypeAccessTokenTTL : { [ key ] : DateInterval ; } = { } ; private readonly availableGrants : { [ key in GrantIdentifier ] : GrantInterface ; } ; private options : AuthorizationServerOptions = { requiresPKCE : true , notBeforeLeeway : 0 , tokenCID : "name" , } ; constructor ( private readonly authCodeRepository , private readonly clientRepository , private readonly tokenRepository , private readonly scopeRepository , private readonly userRepository , private readonly jwt , options ? ) { this . setOptions ( options ) ; const repos = [ this . authCodeRepository , this . clientRepository , this . tokenRepository , this . scopeRepository , this . userRepository , this . jwt , ] ; this . availableGrants = { authorization_code : new AuthCodeGrant ( ... repos ) , client_credentials : new ClientCredentialsGrant ( ... repos ) , implicit : new ImplicitGrant ( ... repos ) , password : new PasswordGrant ( ... repos ) , refresh_token : new RefreshTokenGrant ( ... repos ) , } ; } setOptions ( options = { } ) { this . options = { ... this . options , ... options } ; } enableGrantTypes ( ... grants ) { grants . forEach ( grant => { if ( typeof grant === "string" ) return this . enableGrantType ( grant ) ; const [ grantType , accessTokenTTL ] = grant ; this . enableGrantType ( grantType , accessTokenTTL ) ; } ) ; } enableGrantType ( grantType , accessTokenTTL = new DateInterval ( "1h" ) ) { const grant = this . availableGrants [ grantType ] ; grant . options = this . options ; this . enabledGrantTypes [ grantType ] = grant ; this . grantTypeAccessTokenTTL [ grantType ] = accessTokenTTL ; } respondToAccessTokenRequest ( req , _deprecated ? ) { if ( ! ! _deprecated && process . env . NODE_ENV !== "production" ) { console . warn ( `deprecation: respondToAccessTokenRequest has removed response parameter and will be removed in 3.0.0.
      
- respondToAccessTokenRequest(request, response)
+ respondToAccessTokenRequest(request)
      ` ) ; } for ( const grantType of Object . values ( this . enabledGrantTypes ) ) { if ( ! grantType . canRespondToAccessTokenRequest ( req ) ) { continue ; } const accessTokenTTL = this . grantTypeAccessTokenTTL [ grantType . identifier ] ; return grantType . respondToAccessTokenRequest ( req , accessTokenTTL ) ; } throw OAuthException . unsupportedGrantType ( ) ; } validateAuthorizationRequest ( req ) { for ( const grant of Object . values ( this . enabledGrantTypes ) ) { if ( grant . canRespondToAuthorizationRequest ( req ) ) { return grant . validateAuthorizationRequest ( req ) ; } } throw OAuthException . unsupportedGrantType ( ) ; } async completeAuthorizationRequest ( authorizationRequest ) { const grant = this . enabledGrantTypes [ authorizationRequest . grantTypeId ] ; return await grant . completeAuthorizationRequest ( authorizationRequest ) ; } param grantType getGrant ( grantType ) { return this . availableGrants [ grantType ] ; } }