1
- import { InternalOAuthError , Strategy } from "passport-oauth2" ;
1
+ import { InternalOAuthError , Strategy , StrategyOptions , VerifyFunction } from "passport-oauth2" ;
2
2
import config from "../../config" ;
3
3
4
- export function parseProfile ( data ) {
5
- const username = extractProfileAttribute ( data , config . oauth2 . userProfileUsernameAttr )
6
- const displayName = extractProfileAttribute ( data , config . oauth2 . userProfileDisplayNameAttr )
7
- const email = extractProfileAttribute ( data , config . oauth2 . userProfileEmailAttr )
8
- const photo = extractProfileAttribute ( data , config . oauth2 . userProfilePhotoAttr )
4
+ interface Oauth2Profile {
5
+ id : string
6
+ username : string
7
+ displayName : string
8
+ email : string
9
+ photo : string
10
+ }
11
+
12
+ export function parseProfile ( data : Record < string , string > ) : Oauth2Profile {
13
+ const username = extractProfileAttribute ( data , config . oauth2 . userProfileUsernameAttr ) as string
14
+ const displayName = extractProfileAttribute ( data , config . oauth2 . userProfileDisplayNameAttr ) as string
15
+ const email = extractProfileAttribute ( data , config . oauth2 . userProfileEmailAttr ) as string
16
+ const photo = extractProfileAttribute ( data , config . oauth2 . userProfilePhotoAttr ) as string
9
17
10
18
if ( ! username ) {
11
19
throw new Error ( 'cannot fetch username: please set correct CMD_OAUTH2_USER_PROFILE_USERNAME_ATTR' )
@@ -20,12 +28,12 @@ export function parseProfile(data) {
20
28
}
21
29
}
22
30
23
- export function extractProfileAttribute ( data , path ) {
31
+ export function extractProfileAttribute ( data : any , path : string ) : string | string [ ] | undefined {
24
32
if ( ! data ) return undefined
25
33
if ( typeof path !== 'string' ) return undefined
26
34
// can handle stuff like `attrs[0].name`
27
- path = path . split ( '.' )
28
- for ( const segment of path ) {
35
+ const pathSegments = path . split ( '.' )
36
+ for ( const segment of pathSegments ) {
29
37
const m = segment . match ( / ( [ \d \w ] + ) \[ ( .* ) \] / )
30
38
if ( ! m ) {
31
39
data = data [ segment ]
@@ -39,26 +47,30 @@ export function extractProfileAttribute(data, path) {
39
47
return data
40
48
}
41
49
50
+ interface OAuth2CustomStrategyOptions extends StrategyOptions {
51
+ userProfileURL
52
+ }
53
+
42
54
export class OAuth2CustomStrategy extends Strategy {
43
- private _userProfileURL : any ;
55
+ private readonly _userProfileURL : string ;
44
56
45
- constructor ( options , verify ) {
57
+ constructor ( options : OAuth2CustomStrategyOptions , verify : VerifyFunction ) {
46
58
options . customHeaders = options . customHeaders || { }
47
59
super ( options , verify )
48
60
this . name = 'oauth2'
49
61
this . _userProfileURL = options . userProfileURL
50
62
this . _oauth2 . useAuthorizationHeaderforGET ( true )
51
63
}
52
64
53
- userProfile ( accessToken , done ) {
54
- this . _oauth2 . get ( this . _userProfileURL , accessToken , function ( err , body , res ) {
65
+ userProfile ( accessToken : string , done : ( err : Error | null , profile ?: Oauth2Profile ) => void ) : void {
66
+ this . _oauth2 . get ( this . _userProfileURL , accessToken , function ( err , body ) {
55
67
if ( err ) {
56
68
return done ( new InternalOAuthError ( 'Failed to fetch user profile' , err ) )
57
69
}
58
70
59
71
let profile , json
60
72
try {
61
- json = JSON . parse ( body as any )
73
+ json = JSON . parse ( body . toString ( ) )
62
74
profile = parseProfile ( json )
63
75
} catch ( ex ) {
64
76
return done ( new InternalOAuthError ( 'Failed to parse user profile' + ex . toString ( ) , null ) )
0 commit comments