/**
 * Module dependencies.
 */ import * as oauth2orize from "oauth2orize" ; import * as passport from "passport" ; import { AccessToken , Client } from "../models" ; import { sign , verify } from "jsonwebtoken" ; import { AuthError } from "../errors/AuthError" ; export class Oauth2 { private server ; private jwtSecret : string | undefined ; constructor ( ) { this . server = oauth2orize . createServer ( ) ; this . serializeClient ( ) ; this . registerGrants ( ) ; this . jwtSecret = process . env . JWT_SECRET ; } public getTokenEndpoint ( ) { return [ passport . authenticate ( [ 'local' ] , { session : false } ) , this . server . token ( ) , this . server . errorHandler ( ) ] ; } private serializeClient ( ) { this . server . serializeClient ( function ( client , done ) { return done ( null , client . id ) ; } ) ; this . server . deserializeClient ( function ( id , done ) { Client . findById ( id ) . then ( function ( client ) { return done ( null , client ) ; } , function ( error ) { return done ( error ) ; } ) ; } ) ; } private registerGrants ( ) { this . registerPasswordGrant ( ) ; } private registerPasswordGrant ( ) { this . server . exchange ( oauth2orize . exchange . password ( ( athlete , username , password , scope , done ) => { AccessToken . findOne < AccessToken > ( { where : { userId : athlete . id } } ) . then ( accessToken => { if ( accessToken ) { if ( this . jwtSecret ) { verify ( accessToken . token , this . jwtSecret , ( err , decodedToken ) => { if ( err ) { accessToken . destroy ( ) . then ( ( ) => { if ( this . jwtSecret ) { sign ( athlete . dataValues , this . jwtSecret , { expiresIn : "10h" } , ( err , encodedToken ) => { if ( err ) { return done ( err ) ; } AccessToken . create ( { token : encodedToken , userId : athlete . id } ) . then ( ( accessToken ) => { return done ( null , accessToken . token ) ; } ) . catch ( ( error ) => { return done ( error ) ; } ) ; } ) ; } else { return done ( new AuthError ( "JWT Secret Undefined" ) , false ) ; } } ) ; } else if ( decodedToken && accessToken . userId === decodedToken . id ) { return done ( null , accessToken . token ) ; } else { return done ( new AuthError ( "Token Validation Error" ) , false ) ; } } ) ; } else { return done ( new AuthError ( "JWT Secret Undefined" ) , false ) ; } } else { if ( this . jwtSecret ) { sign ( athlete . dataValues , this . jwtSecret , { expiresIn : "10h" } , ( err , encodedToken ) => { if ( err ) { return done ( err ) ; } AccessToken . create ( { token : encodedToken , userId : athlete . id } ) . then ( ( accessToken ) => { return done ( null , accessToken . token ) ; } ) . catch ( ( error ) => { return done ( new AuthError ( error . message ) ) ; } ) ; } ) ; } else { return done ( new AuthError ( "JWT Secret Undefined" ) ) ; } } } ) ; } ) ) ; } private registerClientCredentialGrant ( ) { this . server . exchange ( oauth2orize . exchange . clientCredentials ( function ( client , scope , done ) { Client . findOne ( { where : { clientId : client . clientId } } ) . then ( function ( localClient ) { if ( localClient === null ) return done ( null , false ) ; if ( localClient . clientSecret !== client . clientSecret ) return done ( null , false ) ; const token = sign ( localClient , this . jwtSecret , { expiresIn : "10h" } ) ; AccessToken . create ( { token : token , clientId : client . dataValues . id } ) . then ( function ( accessToken ) { return done ( null , accessToken ) ; } ) . catch ( function ( error ) { return done ( error ) ; } ) ; } ) . catch ( function ( error ) { return done ( error ) ; } ) ; } ) ) ; } }