import OAuth2Server = require ( "oauth2-server" ) ; import { getRepository } from "typeorm" ; import { Client } from "./client" ; import { User , User as UserEntity } from "./user" ; import { AccessToken } from "./access-token" ; export const oauth2Model = { getClient : async ( clientId , clientSecret ) : Promise < Client | '' | 0 | false | null | undefined > => { let client = await getRepository ( Client ) . createQueryBuilder ( 'c' ) . where ( 'c.id = :clientId AND c.secret = :clientSecret' , { clientId : clientId , clientSecret : clientSecret } ) . getOne ( ) ; if ( ! client ) { return Promise . reject ( "The combination of client id and client secret are incorrect" ) ; } return Promise . resolve ( client ) ; } , saveToken : async ( token , client , user ) : Promise < AccessToken | '' | 0 | false | null | undefined > => { token . client = client ; token . clientId = client . id ; token . user = user ; token . userId = user . id ; token . createdFrom = '' ; let bearer = await getRepository ( AccessToken ) . save ( token ) ; if ( ! bearer ) { return Promise . reject ( "Unable to create the token" ) ; } return Promise . resolve ( bearer ) ; } , getAccessToken : async ( accessToken ) : Promise < AccessToken > => { let token = await getRepository ( AccessToken ) . createQueryBuilder ( 'at' ) . where ( 'at.access_token = :accessToken' , { accessToken : accessToken } ) . getOne ( ) ; if ( ! token ) { return Promise . reject ( "Token not found" ) ; } return Promise . resolve ( token ) ; } , verifyScope : async ( token , scope ) : Promise < boolean > => { return true ; } , getUser : async ( email , password ) : Promise < User | '' | 0 | false | null | undefined > => { let user = await getRepository ( UserEntity ) . createQueryBuilder ( 'u' ) . where ( 'u.email = :email AND u.password = :password' , { email : email , password : password } ) . getOne ( ) ; if ( ! user ) { return Promise . reject ( "User not found" ) ; } return Promise . resolve ( user ) ; } } ;