Skip to content

Commit

Permalink
feat: 🔥 修复 miniapp code 过期问题 以及支持 web sdk refresh_token
Browse files Browse the repository at this point in the history
  • Loading branch information
周雅风 committed Mar 6, 2024
1 parent f5abd09 commit c80d59d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 9 deletions.
15 changes: 9 additions & 6 deletions packages/miniapp/src/Authing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,15 @@ export class Authing {
}

try {
await AuthingMove.checkSession()
const code = await this.getCachedWxLoginCode()
if (!code) {
await next()
}
} catch (e) {
/** checkSession 并不能对 login 的 code 有效性进行 check
* eg: Authing 实例化后会调用 login 返回 code 此时不进行操作 大概十分钟后调用接口 如 loginByCode 微信端返回 code 失效
*/
// await AuthingMove.checkSession()
// const code = await this.getCachedWxLoginCode()
// if (!code) {
// await next()
// }
// } catch (e) {
this.storage.remove(getWxLoginCodeKey(this.options.appId))
await next()
} finally {
Expand Down
53 changes: 51 additions & 2 deletions packages/web/src/Authing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,10 +407,10 @@ export class Authing {
'获取登录流程会话失败, 请确认是否重复访问了回调端点,以及浏览器是否支持 sessionStorage'
)
}

// implicit flow
const idToken = paramDict.id_token
const accessToken = paramDict.access_token
const refreshToken = paramDict.refresh_token
const nonce = tx?.nonce

if (
Expand All @@ -423,6 +423,7 @@ export class Authing {
const result = await this.saveLoginState({
idToken,
accessToken,
refreshToken,
nonce
})

Expand All @@ -431,6 +432,7 @@ export class Authing {
}

return { ...result, customState }

}

/**
Expand Down Expand Up @@ -677,6 +679,49 @@ export class Authing {
)
return
}
/**
*
* 使用内部维护的 refresh_token 刷新 access_token、id_token
*
*/
async refreshToken(): Promise<null | LoginState> {
const state = await this.loginStateProvider.get(
loginStateKey(this.options.appId)
)
if (!state?.refreshToken) {
throw new Error(
'获取 refresh_token 失败,请检查相关协议配置,是否开启 refresh_token 相关功能'
)
}
if (state && state.expireAt && state.expireAt > Date.now()) {
const data = {
grant_type: 'refresh_token',
redirect_uri: '',
refresh_token: state.refreshToken
}

const { data: tokenRes } = (await axiosPost(
`${this.domain}/oidc/token`,
createQueryParams(data),
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'x-authing-app-id': this.options.appId
}
}
)) as { data: OIDCTokenResponse }

// 清掉旧的登录态
await this.loginStateProvider.delete(loginStateKey(this.options.appId))

return this.saveLoginState({
idToken: tokenRes.id_token,
accessToken: tokenRes.access_token,
refreshToken: tokenRes.refresh_token
})
}
return null
}

private async listenToPostMessage(state: string) {
return new Promise<OIDCWebMessageResponse>((resolve, reject) => {
Expand Down Expand Up @@ -720,12 +765,14 @@ export class Authing {
private async saveLoginState(params: {
accessToken?: string
idToken?: string
refreshToken?: string
nonce?: string
}) {
const { accessToken, idToken } = params
const { accessToken, idToken, refreshToken } = params
const loginState: LoginState = {
accessToken: accessToken,
idToken: idToken,
refreshToken: refreshToken,
timestamp: Date.now()
}

Expand Down Expand Up @@ -779,6 +826,7 @@ export class Authing {
return this.saveLoginState({
idToken: tokenRes.id_token,
accessToken: tokenRes.access_token,
refreshToken: tokenRes.refresh_token,
nonce
})
}
Expand All @@ -804,6 +852,7 @@ export class Authing {
return this.saveLoginState({
accessToken: res.accessToken,
idToken: res.idToken,
refreshToken: res.refreshToken,
nonce
})
}
Expand Down
10 changes: 9 additions & 1 deletion packages/web/src/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export interface AuthingSPAInitOptions {
export interface LoginState {
accessToken?: string
idToken?: string
refreshToken?:string
parsedIdToken?: IDToken
parsedAccessToken?: AccessToken
expireAt?: number
Expand Down Expand Up @@ -247,4 +248,11 @@ export interface NormalError {
apiCode: number
message: string
statusCode: number
}
}


declare global {
interface Window{
crossOriginIsolated?:boolean
}
}

0 comments on commit c80d59d

Please sign in to comment.