import oauth2orize , { OAuth2Server , SerializeClientDoneFunction , DeserializeClientDoneFunction , ExchangeDoneFunction } from "oauth2orize" ; import "./passport-provider" ; import Client from "../models/OAuth/Client" ; import ClientCollection from "../models/OAuth/ClientCollection" ; import AuthCode from "../models/OAuth/AuthCode" ; import AuthCodeCollection from "../models/OAuth/AuthCodeCollection" ; import AccessToken from "../models/OAuth/AccessToken" ; import AccessTokenCollection from "../models/OAuth/AccessTokenCollection" ; import UserDocument from "../models/User/UserDocument" ; import * as random from "../util/random" ; import UserCollection from "../models/User/UserCollection" ; const server = oauth2orize . createServer ( ) ; const issueToken = ( clientId , userId , done ) : void => { console . log ( "[issue token]" ) ; const token = random . getUid ( 256 ) ; const accessToken = new AccessTokenCollection ( { token : token , clientId : clientId , userId : userId } ) ; accessToken . save ( ( error , accessToken ) : void => { if ( error ) { return done ( error , undefined ) ; } return done ( null , accessToken . token ) ; } ) ; } ; server . serializeClient ( ( client , done ) => done ( null , client . id ) ) ; server . deserializeClient ( ( id , done ) => { done ( null , ClientCollection . find ( ( client ) => client . id === id ) ) ; } ) ; server . grant ( oauth2orize . grant . code ( ( client , redirectUri , user , res , issued ) => { console . log ( "[oauth2orize.grant.code]" ) ; const code = random . getUid ( 16 ) ; const authCode = new AuthCodeCollection ( { code : code , clientId : client . id , userId : user . id , userName : user . name , redirectUri : redirectUri } ) ; authCode . save ( ( error , authCode ) : void => { if ( error ) { return issued ( error , undefined ) ; } return issued ( null , authCode . code ) ; } ) ; } ) ) ; server . grant ( oauth2orize . grant . token ( ( client , user , res , done ) => { console . log ( "[oauth2orize.grant.token]" ) ; issueToken ( client . id , user . id , done ) ; } ) ) ; server . exchange ( oauth2orize . exchange . code ( ( client , code , redirectUri , done ) => { console . log ( "[oauth2orize.exchange.code]" ) ; AuthCodeCollection . findOne ( { code : code } , ( error , authCode ) => { if ( error ) { return done ( error ) ; } if ( client . id !== authCode . clientId ) { return done ( null , false ) ; } if ( redirectUri !== authCode . redirectUri ) { return done ( null , false ) ; } issueToken ( client . id , authCode . userId , done ) ; } ) ; } ) ) ; server . exchange ( oauth2orize . exchange . password ( ( client , email , password , scope , done ) => { console . log ( "[oauth2orize.exchange.password]" ) ; const foundClient = ClientCollection . find ( ( value ) => client . id === value . id ) ; if ( ! foundClient || foundClient . secret !== client . secret ) { return done ( null , false ) ; } UserCollection . findOne ( { email : email . toLowerCase ( ) } , ( err , user ) : void => { if ( err ) { return done ( err ) ; } if ( ! user ) { return done ( null , false ) ; } user . comparePassword ( password , ( err , isMatch ) => { if ( err ) { return done ( err ) ; } if ( ! isMatch ) { return done ( null , false ) ; } issueToken ( client . id , user . id , done ) ; } ) ; } ) ; } ) ) ; server . exchange ( oauth2orize . exchange . clientCredentials ( ( client , scope , done ) => { console . log ( "[oauth2orize.exchange.clientCredentials]" ) ; const foundClient = ClientCollection . find ( ( value ) => client . id === value . id ) ; if ( ! foundClient || foundClient . secret !== client . secret ) { return done ( null , false ) ; } issueToken ( client . id , "" , done ) ; } ) ) ; export default server ;