@@ -8,6 +8,7 @@ import { CloudSessionService } from 'src/modules/cloud/session/cloud-session.ser
8
8
import { GithubIdpCloudAuthStrategy } from 'src/modules/cloud/auth/auth-strategy/github-idp.cloud.auth-strategy' ;
9
9
import { wrapHttpError } from 'src/common/utils' ;
10
10
import {
11
+ CloudOauthGithubEmailPermissionException ,
11
12
CloudOauthMisconfigurationException , CloudOauthMissedRequiredDataException ,
12
13
CloudOauthUnknownAuthorizationRequestException ,
13
14
} from 'src/modules/cloud/auth/exceptions' ;
@@ -32,11 +33,23 @@ export class CloudAuthService {
32
33
private readonly eventEmitter : EventEmitter2 ,
33
34
) { }
34
35
35
- static getAuthorizationServerRedirectError ( query : { error_description : string } ) {
36
- if ( query ?. error_description ?. indexOf ( 'properties are missing' ) > - 1 ) {
37
- return new CloudOauthMissedRequiredDataException ( query . error_description , {
38
- description : query . error_description ,
39
- } ) ;
36
+ static getAuthorizationServerRedirectError (
37
+ query : { error_description : string , error : string } ,
38
+ authRequest ?: CloudAuthRequest ,
39
+ ) {
40
+ if (
41
+ query ?. error_description ?. indexOf ( 'propert' ) > - 1
42
+ && query ?. error_description ?. indexOf ( 'required' ) > - 1
43
+ && query ?. error_description ?. indexOf ( 'miss' ) > - 1
44
+ ) {
45
+ return (
46
+ authRequest ?. idpType === CloudAuthIdpType . GitHub
47
+ && query ?. error_description ?. indexOf ( 'email' ) > - 1
48
+ )
49
+ ? new CloudOauthGithubEmailPermissionException ( query . error_description )
50
+ : new CloudOauthMissedRequiredDataException ( query . error_description , {
51
+ description : query . error_description ,
52
+ } ) ;
40
53
}
41
54
42
55
return new CloudOauthMisconfigurationException ( undefined , {
@@ -68,17 +81,21 @@ export class CloudAuthService {
68
81
callback ?: Function ,
69
82
} ,
70
83
) : Promise < string > {
71
- const authRequest : any = await this . getAuthStrategy ( options ?. strategy ) . generateAuthRequest ( sessionMetadata ) ;
72
- authRequest . callback = options ?. callback ;
73
- authRequest . action = options ?. action ;
84
+ try {
85
+ const authRequest : any = await this . getAuthStrategy ( options ?. strategy ) . generateAuthRequest ( sessionMetadata ) ;
86
+ authRequest . callback = options ?. callback ;
87
+ authRequest . action = options ?. action ;
74
88
75
- // based on requirements we must support only single auth request at the moment
76
- // and logout user before
77
- await this . logout ( sessionMetadata ) ;
78
- this . authRequests . clear ( ) ;
79
- this . authRequests . set ( authRequest . state , authRequest ) ;
89
+ // based on requirements we must support only single auth request at the moment
90
+ // and logout user before
91
+ await this . logout ( sessionMetadata ) ;
92
+ this . authRequests . clear ( ) ;
93
+ this . authRequests . set ( authRequest . state , authRequest ) ;
80
94
81
- return CloudAuthStrategy . generateAuthUrl ( authRequest ) . toString ( ) ;
95
+ return CloudAuthStrategy . generateAuthUrl ( authRequest ) . toString ( ) ;
96
+ } catch ( e ) {
97
+ throw new CloudOauthMisconfigurationException ( ) ;
98
+ }
82
99
}
83
100
84
101
/**
@@ -137,12 +154,12 @@ export class CloudAuthService {
137
154
throw new CloudOauthUnknownAuthorizationRequestException ( ) ;
138
155
}
139
156
157
+ const authRequest = this . authRequests . get ( query . state ) ;
158
+
140
159
if ( query ?. error ) {
141
- throw CloudAuthService . getAuthorizationServerRedirectError ( query ) ;
160
+ throw CloudAuthService . getAuthorizationServerRedirectError ( query , authRequest ) ;
142
161
}
143
162
144
- const authRequest = this . authRequests . get ( query . state ) ;
145
-
146
163
// delete authRequest on this step
147
164
// allow to redirect with authorization code only once
148
165
this . authRequests . delete ( query . state ) ;
0 commit comments