2020-04-17 00:59:48 +00:00
const Oauth2Controller = require ( 'flitter-auth/controllers/Oauth2' )
/ *
* Handles views , processing , and data retrieval for flitter - auth ' s
* built - in OAuth2 server , if it is enabled . Most handlers are inherited
* from flitter - auth / controllers / Oauth2 , but you can override them here
* as you need .
* /
class Oauth2 extends Oauth2Controller {
2020-05-18 02:13:38 +00:00
static get services ( ) {
return [ ... super . services , 'Vue' , 'configs' , 'models' ]
}
async authorize _post ( req , res , next ) {
const client = await this . _get _authorize _client ( { query : req . body } )
if ( ! client ) return this . _uniform ( res , 'Unable to authorize client application. The application config is invalid. Please check the client ID and redirect URI and try again.' )
const StarshipClient = this . models . get ( 'oauth:Client' )
const starship _client = await StarshipClient . findOne ( { active : true , uuid : client . clientID } )
req . user . authorize ( starship _client )
await req . user . save ( )
return super . authorize _post ( req , res , next )
}
async authorize _get ( req , res , next ) {
const client = await this . _get _authorize _client ( req )
if ( ! client ) return this . _uniform ( res , 'Unable to authorize client application. The application config is invalid. Please check the client ID and redirect URI and try again.' )
const uri = new URL ( req . query . redirect _uri )
const StarshipClient = this . models . get ( 'oauth:Client' )
const starship _client = await StarshipClient . findOne ( { active : true , uuid : client . clientID } )
if ( req . user . has _authorized ( starship _client ) ) {
return this . Vue . invoke _action ( res , {
text : 'Grant Access' ,
action : 'post' ,
params : {
redirect _uri : uri . toString ( ) ,
client _id : client . clientID ,
} ,
} )
}
return res . page ( 'public:message' , {
... this . Vue . data ( {
message : ` <h3 class="font-weight-light">Authorize ${ client . name } ?</h3>
< br >
$ { client . name } is requesting access to your $ { this . configs . get ( 'app.name' ) } account . Once you grant it , you may not be prompted for permission again .
< br > < br > < br >
< i > < small > You will be redirected to : $ { uri . host } < /small></i > ` ,
actions : [
{
text : 'Deny' ,
action : 'redirect' ,
next : '/dash' ,
} ,
{
text : 'Grant Access' ,
action : 'post' ,
params : {
redirect _uri : uri . toString ( ) ,
client _id : client . clientID ,
} ,
} ,
] ,
} )
} )
}
2020-04-17 00:59:48 +00:00
}
module . exports = exports = Oauth2