@@ -8,7 +8,10 @@ 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
+ CloudOauthCanceledException ,
12
+ CloudOauthGithubEmailPermissionException ,
11
13
CloudOauthMisconfigurationException , CloudOauthMissedRequiredDataException ,
14
+ CloudOauthUnexpectedErrorException ,
12
15
CloudOauthUnknownAuthorizationRequestException ,
13
16
} from 'src/modules/cloud/auth/exceptions' ;
14
17
import { CloudAuthRequestInfo , CloudAuthResponse , CloudAuthStatus } from 'src/modules/cloud/auth/models' ;
@@ -32,14 +35,30 @@ export class CloudAuthService {
32
35
private readonly eventEmitter : EventEmitter2 ,
33
36
) { }
34
37
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
- } ) ;
38
+ static getAuthorizationServerRedirectError (
39
+ query : { error_description : string , error : string } ,
40
+ authRequest ?: CloudAuthRequest ,
41
+ ) {
42
+ if ( query ?. error_description ?. indexOf ( 'canceled' ) > - 1 ) {
43
+ return new CloudOauthCanceledException ( ) ;
44
+ }
45
+
46
+ if (
47
+ query ?. error_description ?. indexOf ( 'propert' ) > - 1
48
+ && query ?. error_description ?. indexOf ( 'required' ) > - 1
49
+ && query ?. error_description ?. indexOf ( 'miss' ) > - 1
50
+ ) {
51
+ return (
52
+ authRequest ?. idpType === CloudAuthIdpType . GitHub
53
+ && query ?. error_description ?. indexOf ( 'email' ) > - 1
54
+ )
55
+ ? new CloudOauthGithubEmailPermissionException ( query . error_description )
56
+ : new CloudOauthMissedRequiredDataException ( query . error_description , {
57
+ description : query . error_description ,
58
+ } ) ;
40
59
}
41
60
42
- return new CloudOauthMisconfigurationException ( undefined , {
61
+ return new CloudOauthUnexpectedErrorException ( undefined , {
43
62
description : query . error_description ,
44
63
} ) ;
45
64
}
@@ -68,17 +87,21 @@ export class CloudAuthService {
68
87
callback ?: Function ,
69
88
} ,
70
89
) : Promise < string > {
71
- const authRequest : any = await this . getAuthStrategy ( options ?. strategy ) . generateAuthRequest ( sessionMetadata ) ;
72
- authRequest . callback = options ?. callback ;
73
- authRequest . action = options ?. action ;
90
+ try {
91
+ const authRequest : any = await this . getAuthStrategy ( options ?. strategy ) . generateAuthRequest ( sessionMetadata ) ;
92
+ authRequest . callback = options ?. callback ;
93
+ authRequest . action = options ?. action ;
74
94
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 ) ;
95
+ // based on requirements we must support only single auth request at the moment
96
+ // and logout user before
97
+ await this . logout ( sessionMetadata ) ;
98
+ this . authRequests . clear ( ) ;
99
+ this . authRequests . set ( authRequest . state , authRequest ) ;
80
100
81
- return CloudAuthStrategy . generateAuthUrl ( authRequest ) . toString ( ) ;
101
+ return CloudAuthStrategy . generateAuthUrl ( authRequest ) . toString ( ) ;
102
+ } catch ( e ) {
103
+ throw new CloudOauthMisconfigurationException ( ) ;
104
+ }
82
105
}
83
106
84
107
/**
@@ -137,12 +160,12 @@ export class CloudAuthService {
137
160
throw new CloudOauthUnknownAuthorizationRequestException ( ) ;
138
161
}
139
162
163
+ const authRequest = this . authRequests . get ( query . state ) ;
164
+
140
165
if ( query ?. error ) {
141
- throw CloudAuthService . getAuthorizationServerRedirectError ( query ) ;
166
+ throw CloudAuthService . getAuthorizationServerRedirectError ( query , authRequest ) ;
142
167
}
143
168
144
- const authRequest = this . authRequests . get ( query . state ) ;
145
-
146
169
// delete authRequest on this step
147
170
// allow to redirect with authorization code only once
148
171
this . authRequests . delete ( query . state ) ;
0 commit comments