forked from hiero-ledger/heka-identity-platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOAuthStore.ts
More file actions
190 lines (150 loc) · 5.68 KB
/
Copy pathOAuthStore.ts
File metadata and controls
190 lines (150 loc) · 5.68 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { GlobalLogger } from '@heka-wallet/shared'
import AsyncStorage from '@react-native-async-storage/async-storage'
import axios from 'axios'
import { makeAutoObservable, runInAction } from 'mobx'
import moment from 'moment'
import { Linking } from 'react-native'
import { AuthConfiguration, authorize, AuthorizeResult, refresh, revoke } from 'react-native-app-auth'
import * as Keychain from 'react-native-keychain'
import { UserInfo } from '../types/auth'
import { WalletKeychainServices } from '../types/keychain'
import { getKeychainAccessOptions } from '../utils/keychain'
export const OAUTH_TOKEN_KEY = 'OAuthToken'
export const OAUTH_USER_INFO_KEY = 'OAuthUserInfo'
export interface OAuthStoreConfig {
oauthConfig: AuthConfiguration
userInfoEndpoint: string
accountDeletionURL: string
}
type AuthState = AuthorizeResult
const logger = GlobalLogger.createContextLogger('OAuth')
export class OAuthStore {
protected static keychainOptions: Keychain.Options = getKeychainAccessOptions(WalletKeychainServices.OAuth)
private _authState: AuthState | null = null
private _userInfo: UserInfo | null = null
private _isInitialized = false
constructor(private readonly _config: OAuthStoreConfig) {
makeAutoObservable(this, {}, { autoBind: true })
this.initialize()
}
get isLoading(): boolean {
return !this._isInitialized
}
get isLoggedIn(): boolean {
return !!this._authState
}
async getAccessToken(): Promise<string> {
if (!this._authState) {
await this.logIn()
} else if (this.isTokenExpired) {
await this.refreshToken().catch(async () => {
logger.warn('Token refresh failed, falling back to login...')
await this.logIn()
})
}
return this._authState!.accessToken
}
async getUserInfo(): Promise<UserInfo> {
await this.refreshUserInfo()
if (!this._userInfo) {
throw new Error('User info is empty')
}
return this._userInfo
}
async logIn(): Promise<void> {
try {
const state = await authorize(this._config.oauthConfig)
logger.debug('Logged in with auth state:', state)
await this.saveAuthState(state)
await this.refreshUserInfo()
} catch (error) {
logger.error('Logging in failed with error:', error)
throw error
}
}
async refreshToken(): Promise<void> {
if (!this._authState?.refreshToken) {
throw new Error('Refresh token is empty')
}
try {
const refreshResult = await refresh(this._config.oauthConfig, { refreshToken: this._authState.refreshToken })
const state = {
...this._authState,
...refreshResult,
refreshToken: refreshResult.refreshToken ?? this._authState.refreshToken,
}
await this.saveAuthState(state)
} catch (error) {
logger.error('Token refresh failed with error:', error)
throw error
}
}
async logOut(): Promise<void> {
if (!this._authState) {
throw new Error('Auth state is empty')
}
try {
// We don't want to prevent user from logging out due to token revocation failure (including being offline), so we're catching possible error here.
// TODO: Add background retry for failed network requests to revoke token when device is back online
await revoke(this._config.oauthConfig, {
tokenToRevoke: this._authState.accessToken,
includeBasicAuth: true,
sendClientId: true,
}).catch((error) => logger.error('Access token revocation failed with error:', error))
await this.resetAuthState()
await this.resetUserInfo()
logger.debug('Successfully logged out')
} catch (error) {
logger.error('Logging out failed with error:', error)
throw error
}
}
// TODO: Consider using embedded WebView for OAuth frontend interaction
openAccountDeletionPage(): Promise<void> {
return Linking.openURL(this._config.accountDeletionURL)
}
private async initialize(): Promise<void> {
const keychainCredentials = await Keychain.getGenericPassword(OAuthStore.keychainOptions)
if (keychainCredentials) {
const authState = JSON.parse(keychainCredentials.password)
logger.debug('Loaded auth state:', authState)
runInAction(() => (this._authState = authState))
}
const userInfoData = await AsyncStorage.getItem(OAUTH_USER_INFO_KEY)
if (userInfoData) {
const userInfo = JSON.parse(userInfoData)
logger.debug('Loaded user info:', userInfo)
runInAction(() => (this._userInfo = userInfo))
}
runInAction(() => (this._isInitialized = true))
}
private get isTokenExpired(): boolean {
if (!this._authState) {
throw new Error('Auth state is empty')
}
return moment(this._authState.accessTokenExpirationDate).isBefore()
}
private async saveAuthState(state: AuthState): Promise<void> {
await Keychain.setGenericPassword(OAUTH_TOKEN_KEY, JSON.stringify(state), OAuthStore.keychainOptions)
runInAction(() => (this._authState = state))
}
private async resetAuthState(): Promise<void> {
await Keychain.resetGenericPassword(OAuthStore.keychainOptions)
runInAction(() => (this._authState = null))
}
private async refreshUserInfo(): Promise<void> {
try {
const accessToken = await this.getAccessToken()
const { data: userInfo } = await axios.get<UserInfo>(this._config.userInfoEndpoint, {
headers: { Authorization: `Bearer ${accessToken}` },
})
await AsyncStorage.setItem(OAUTH_USER_INFO_KEY, JSON.stringify(userInfo))
runInAction(() => (this._userInfo = userInfo))
} catch (error) {
logger.error('Refreshing user info failed with error:', error)
}
}
private resetUserInfo(): Promise<void> {
return AsyncStorage.removeItem(OAUTH_USER_INFO_KEY)
}
}