Skip to content

Commit 5dbdbd8

Browse files
committed
lint: lib/auth/oauth2/strategy.ts
- add typing annotate
1 parent 3ac8993 commit 5dbdbd8

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

lib/auth/oauth2/strategy.ts

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1-
import {InternalOAuthError, Strategy} from "passport-oauth2";
1+
import {InternalOAuthError, Strategy, StrategyOptions, VerifyFunction} from "passport-oauth2";
22
import config from "../../config";
33

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
917

1018
if (!username) {
1119
throw new Error('cannot fetch username: please set correct CMD_OAUTH2_USER_PROFILE_USERNAME_ATTR')
@@ -20,12 +28,12 @@ export function parseProfile(data) {
2028
}
2129
}
2230

23-
export function extractProfileAttribute(data, path) {
31+
export function extractProfileAttribute(data: any, path: string): string | string[] | undefined {
2432
if (!data) return undefined
2533
if (typeof path !== 'string') return undefined
2634
// 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) {
2937
const m = segment.match(/([\d\w]+)\[(.*)\]/)
3038
if (!m) {
3139
data = data[segment]
@@ -39,26 +47,30 @@ export function extractProfileAttribute(data, path) {
3947
return data
4048
}
4149

50+
interface OAuth2CustomStrategyOptions extends StrategyOptions {
51+
userProfileURL
52+
}
53+
4254
export class OAuth2CustomStrategy extends Strategy {
43-
private _userProfileURL: any;
55+
private readonly _userProfileURL: string;
4456

45-
constructor(options, verify) {
57+
constructor(options: OAuth2CustomStrategyOptions, verify: VerifyFunction) {
4658
options.customHeaders = options.customHeaders || {}
4759
super(options, verify)
4860
this.name = 'oauth2'
4961
this._userProfileURL = options.userProfileURL
5062
this._oauth2.useAuthorizationHeaderforGET(true)
5163
}
5264

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) {
5567
if (err) {
5668
return done(new InternalOAuthError('Failed to fetch user profile', err))
5769
}
5870

5971
let profile, json
6072
try {
61-
json = JSON.parse(body as any)
73+
json = JSON.parse(body.toString())
6274
profile = parseProfile(json)
6375
} catch (ex) {
6476
return done(new InternalOAuthError('Failed to parse user profile' + ex.toString(), null))

0 commit comments

Comments
 (0)