forked from devhubapp/devhub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.ts
More file actions
80 lines (68 loc) · 1.98 KB
/
auth.ts
File metadata and controls
80 lines (68 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import qs from 'qs'
import { GitHubTokenDetails } from '@devhub/core'
import { Linking } from '../../libs/linking'
import { OAuthResponseData } from '../../libs/oauth/helpers'
import { Platform } from '../../libs/platform'
export function clearQueryStringFromURL(fields: string[]) {
if (
!(
Platform.OS === 'web' &&
!Platform.isElectron &&
typeof window !== 'undefined' &&
window.history &&
window.history.replaceState &&
window.location
)
)
return
const query = window.location.search.replace(new RegExp(`^[?]?`), '')
const params = qs.parse(query)
fields.forEach(field => {
delete params[field]
})
window.history.replaceState(
{},
document.title,
`${window.location.pathname || '/'}${
Object.keys(params).length ? `?${qs.stringify(params)}` : ''
}`,
)
}
export function clearOAuthQueryParams() {
Linking.clearCurrentURL()
clearQueryStringFromURL([
'app_token',
'code',
'github_app_type',
'github_scope',
'github_token',
'github_token_created_at',
'github_token_type',
'oauth',
])
}
export function tryParseOAuthParams(
params: OAuthResponseData,
): { appToken?: string; tokenDetails?: GitHubTokenDetails } {
try {
if (!(params && params.app_token && params.github_token))
throw new Error('No token received.')
const appToken = params.app_token
const githubScope = params.github_scope
const githubToken = params.github_token
const githubTokenCreatedAt =
params.github_token_created_at || new Date().toISOString()
const githubTokenType = params.github_token_type || 'bearer'
const tokenDetails: GitHubTokenDetails = {
scope: githubScope,
token: githubToken,
tokenType: githubTokenType,
tokenCreatedAt: githubTokenCreatedAt,
}
clearOAuthQueryParams()
return { appToken, tokenDetails }
} catch (error) {
if (error.message === 'Canceled' || error.message === 'Timeout') return {}
throw error
}
}