@@ -14,6 +14,7 @@ import { JwtService } from '@nestjs/jwt';
1414import { resolve } from 'path' ;
1515import { existsSync , readFileSync , writeFileSync } from 'fs' ;
1616import { ConsoleSession } from '~/_common/data/console-session' ;
17+ import { KeyringsService } from '../keyrings/keyrings.service' ;
1718
1819@Injectable ( )
1920export class AuthService extends AbstractService implements OnModuleInit {
@@ -29,6 +30,7 @@ export class AuthService extends AbstractService implements OnModuleInit {
2930 public constructor (
3031 protected moduleRef : ModuleRef ,
3132 protected readonly agentsService : AgentsService ,
33+ protected readonly keyringsService : KeyringsService ,
3234 private readonly jwtService : JwtService ,
3335 @InjectRedis ( ) private readonly redis : Redis ,
3436 ) {
@@ -52,6 +54,7 @@ export class AuthService extends AbstractService implements OnModuleInit {
5254 }
5355 const { access_token } = await this . createTokens ( new ConsoleSession ( ) , false , {
5456 expiresIn : '1y' ,
57+ scopes : [ 'offline' , 'api' ] ,
5558 } ) ;
5659 writeFileSync (
5760 devTokenPath ,
@@ -78,17 +81,28 @@ export class AuthService extends AbstractService implements OnModuleInit {
7881 }
7982
8083 // eslint-disable-next-line
81- public async verifyIdentity ( payload : any & { identity : AgentType } ) : Promise < any > {
84+ public async verifyIdentity ( payload : any & { identity : AgentType & { token : string } } ) : Promise < any > {
85+ if ( payload . scopes . includes ( 'offline' ) ) {
86+ return payload . identity ;
87+ }
88+ if ( payload . scopes . includes ( 'api' ) ) {
89+ try {
90+ const identity = await this . keyringsService . findOne ( {
91+ _id : payload . identity . _id ,
92+ token : payload . identity . token ,
93+ } ) ;
94+ if ( identity ) {
95+ return identity . toObject ( ) ;
96+ }
97+ } catch ( e ) { }
98+ return null ;
99+ }
82100 try {
83- if ( payload . scopes . includes ( 'offline' ) ) {
84- return payload . identity ;
85- }
86101 const identity = await this . redis . get ( [ this . ACCESS_TOKEN_PREFIX , payload . jti ] . join ( ':' ) ) ;
87102 if ( identity ) {
88103 return JSON . parse ( identity ) ;
89104 }
90- } finally {
91- }
105+ } catch ( e ) { }
92106 return null ;
93107 }
94108
@@ -101,15 +115,15 @@ export class AuthService extends AbstractService implements OnModuleInit {
101115 refresh_token ?: string ;
102116 } > {
103117 const scopes = [ 'sesame' ] ;
104- if ( refresh_token === false ) scopes . push ( 'offline' ) ;
118+ if ( options ?. scopes ) scopes . push ( ... options . scopes ) ;
105119 const jwtid = `${ identity . _id } _${ randomBytes ( 16 ) . toString ( 'hex' ) } ` ;
106120 const access_token = this . jwtService . sign (
107121 { identity, scopes } ,
108122 {
109123 expiresIn : this . ACCESS_TOKEN_EXPIRES_IN ,
110124 jwtid,
111125 subject : `${ identity . _id } ` ,
112- ...options ,
126+ ...omit ( options , [ 'scopes' ] ) ,
113127 } ,
114128 ) ;
115129 if ( refresh_token === false ) return { access_token } ;
0 commit comments