@@ -50,6 +50,7 @@ import { GetStatusResponseModel } from './model/get-status.response.model';
5050import { ILogin , IRegister } from './interfaces' ;
5151
5252const scryptAsync = promisify ( scrypt ) ;
53+ const REMNAWAVE_CUSTOM_CLAIM_KEY = 'remnawaveAccess' ;
5354
5455@Injectable ( )
5556export class AuthService {
@@ -447,7 +448,7 @@ export class AuthService {
447448 authorizationURL = pocketIdClient . createAuthorizationURL (
448449 `https://${ remnawaveSettings . oauth2Settings . pocketid . plainDomain } /authorize` ,
449450 state ,
450- [ 'email' ] ,
451+ [ 'email' , 'profile' ] ,
451452 ) ;
452453 stateKey = `oauth2:${ OAUTH2_PROVIDERS . POCKETID } ` ;
453454 break ;
@@ -727,10 +728,7 @@ export class AuthService {
727728 userAgent ,
728729 'PocketID OAuth2 state mismatch.' ,
729730 ) ;
730- return {
731- isAllowed : false ,
732- email : null ,
733- } ;
731+ return { isAllowed : false , email : null } ;
734732 }
735733
736734 const remnawaveSettings = await this . queryBus . execute (
@@ -749,63 +747,43 @@ export class AuthService {
749747 null ,
750748 ) ;
751749
752- const accessToken = tokens . accessToken ( ) ;
753-
754- const { data } = await firstValueFrom (
755- this . httpService
756- . get < {
757- email : string ;
758- email_verified : boolean ;
759- sub : string ;
760- } > (
761- `https://${ remnawaveSettings . oauth2Settings . pocketid . plainDomain } /api/oidc/userinfo` ,
762- {
763- headers : {
764- Authorization : `Bearer ${ accessToken } ` ,
765- 'User-Agent' : 'Remnawave' ,
766- } ,
767- } ,
768- )
769- . pipe (
770- catchError ( ( error : AxiosError ) => {
771- throw error . response ?. data ;
772- } ) ,
773- ) ,
774- ) ;
750+ const claims = arctic . decodeIdToken ( tokens . idToken ( ) ) ;
775751
776- if ( ! data ) {
777- this . logger . error ( 'Failed to fetch PocketID user info' ) ;
778- return {
779- isAllowed : false ,
780- email : null ,
781- } ;
782- }
783-
784- if ( ! remnawaveSettings . oauth2Settings . pocketid . allowedEmails . includes ( data . email ) ) {
752+ const email = 'email' in claims ? claims . email : undefined ;
753+ if ( typeof email !== 'string' || ! email ) {
785754 await this . emitFailedLoginAttempt (
786- data . email ,
755+ 'Missing' ,
787756 '–' ,
788757 ip ,
789758 userAgent ,
790- 'PocketID email is not in the allowed list .' ,
759+ 'Invalid or missing email claim in PocketID ID token .' ,
791760 ) ;
792- return {
793- isAllowed : false ,
794- email : null ,
795- } ;
761+ return { isAllowed : false , email : null } ;
796762 }
797763
798- return {
799- isAllowed : true ,
800- email : data . email ,
801- } ;
764+ if (
765+ REMNAWAVE_CUSTOM_CLAIM_KEY in claims &&
766+ claims [ REMNAWAVE_CUSTOM_CLAIM_KEY ] === true
767+ ) {
768+ return { isAllowed : true , email } ;
769+ }
770+
771+ if ( remnawaveSettings . oauth2Settings . pocketid . allowedEmails . includes ( email ) ) {
772+ return { isAllowed : true , email } ;
773+ }
774+
775+ await this . emitFailedLoginAttempt (
776+ email ,
777+ '–' ,
778+ ip ,
779+ userAgent ,
780+ 'PocketID email is not in the allowed list and remnawaveClaim is not present.' ,
781+ ) ;
782+
783+ return { isAllowed : false , email : null } ;
802784 } catch ( error ) {
803785 this . logger . error ( `PocketID callback error: ${ error } ` ) ;
804-
805- return {
806- isAllowed : false ,
807- email : null ,
808- } ;
786+ return { isAllowed : false , email : null } ;
809787 }
810788 }
811789
0 commit comments