155 lines
4.0 KiB
JavaScript
155 lines
4.0 KiB
JavaScript
const { STORAGE_KEYS, getStorage, removeStorage, setStorage } = require('./storage')
|
|
const SIGNED_URL_SAFE_BUFFER_MS = 60 * 1000
|
|
|
|
function parseSignedUrlExpireAt(url) {
|
|
const rawUrl = String(url || '').trim()
|
|
if (!rawUrl || rawUrl.indexOf('?') < 0) {
|
|
return 0
|
|
}
|
|
|
|
const queryString = rawUrl.slice(rawUrl.indexOf('?') + 1)
|
|
const params = {}
|
|
queryString.split('&').forEach(segment => {
|
|
if (!segment) {
|
|
return
|
|
}
|
|
const dividerIndex = segment.indexOf('=')
|
|
const key = dividerIndex >= 0 ? segment.slice(0, dividerIndex) : segment
|
|
const value = dividerIndex >= 0 ? segment.slice(dividerIndex + 1) : ''
|
|
params[decodeURIComponent(key)] = decodeURIComponent(value)
|
|
})
|
|
|
|
const awsDate = params['X-Amz-Date']
|
|
const awsExpireSeconds = Number(params['X-Amz-Expires'] || 0)
|
|
if (awsDate && awsExpireSeconds > 0 && awsDate.length >= 16) {
|
|
const year = Number(awsDate.slice(0, 4))
|
|
const month = Number(awsDate.slice(4, 6)) - 1
|
|
const day = Number(awsDate.slice(6, 8))
|
|
const hour = Number(awsDate.slice(9, 11))
|
|
const minute = Number(awsDate.slice(11, 13))
|
|
const second = Number(awsDate.slice(13, 15))
|
|
const baseTimestamp = Date.UTC(year, month, day, hour, minute, second)
|
|
if (!Number.isNaN(baseTimestamp)) {
|
|
return baseTimestamp + awsExpireSeconds * 1000
|
|
}
|
|
}
|
|
|
|
const expires = Number(params.Expires || 0)
|
|
if (expires > 0) {
|
|
return expires * 1000
|
|
}
|
|
|
|
return 0
|
|
}
|
|
|
|
function sanitizeStoredUserInfo(userInfo) {
|
|
if (!userInfo || typeof userInfo !== 'object') {
|
|
return userInfo
|
|
}
|
|
|
|
const nextUserInfo = {
|
|
...userInfo
|
|
}
|
|
const avatarExpireAt = parseSignedUrlExpireAt(nextUserInfo.avatar)
|
|
if (avatarExpireAt > 0 && avatarExpireAt <= Date.now() + SIGNED_URL_SAFE_BUFFER_MS) {
|
|
nextUserInfo.avatar = ''
|
|
}
|
|
return nextUserInfo
|
|
}
|
|
|
|
function getAccessToken() {
|
|
return getStorage(STORAGE_KEYS.ACCESS_TOKEN, '')
|
|
}
|
|
|
|
function getRefreshToken() {
|
|
return getStorage(STORAGE_KEYS.REFRESH_TOKEN, '')
|
|
}
|
|
|
|
function getStoredUserInfo() {
|
|
const userInfo = sanitizeStoredUserInfo(getStorage(STORAGE_KEYS.USER_INFO, null))
|
|
if (userInfo) {
|
|
setStorage(STORAGE_KEYS.USER_INFO, userInfo)
|
|
}
|
|
return userInfo
|
|
}
|
|
|
|
function getNeedSetPassword() {
|
|
return Boolean(getStorage(STORAGE_KEYS.NEED_SET_PASSWORD, false))
|
|
}
|
|
|
|
function getPreferredMiniLoginShopId() {
|
|
return getStorage(STORAGE_KEYS.PREFERRED_MINI_LOGIN_SHOP_ID, '')
|
|
}
|
|
|
|
function isLoggedIn() {
|
|
return Boolean(getAccessToken())
|
|
}
|
|
|
|
function setSession(payload) {
|
|
if (!payload || !payload.accessToken || !payload.refreshToken) {
|
|
return
|
|
}
|
|
|
|
setStorage(STORAGE_KEYS.ACCESS_TOKEN, payload.accessToken)
|
|
setStorage(STORAGE_KEYS.REFRESH_TOKEN, payload.refreshToken)
|
|
|
|
if (payload.userInfo) {
|
|
setStorage(STORAGE_KEYS.USER_INFO, sanitizeStoredUserInfo(payload.userInfo))
|
|
}
|
|
|
|
if (payload.needSetPassword) {
|
|
setStorage(STORAGE_KEYS.NEED_SET_PASSWORD, true)
|
|
} else {
|
|
removeStorage(STORAGE_KEYS.NEED_SET_PASSWORD)
|
|
}
|
|
}
|
|
|
|
function updateStoredUserInfo(userInfo) {
|
|
if (!userInfo) {
|
|
removeStorage(STORAGE_KEYS.USER_INFO)
|
|
return
|
|
}
|
|
|
|
setStorage(STORAGE_KEYS.USER_INFO, sanitizeStoredUserInfo(userInfo))
|
|
}
|
|
|
|
function setNeedSetPassword(value) {
|
|
if (value) {
|
|
setStorage(STORAGE_KEYS.NEED_SET_PASSWORD, true)
|
|
return
|
|
}
|
|
|
|
removeStorage(STORAGE_KEYS.NEED_SET_PASSWORD)
|
|
}
|
|
|
|
function clearSession() {
|
|
removeStorage(STORAGE_KEYS.ACCESS_TOKEN)
|
|
removeStorage(STORAGE_KEYS.REFRESH_TOKEN)
|
|
removeStorage(STORAGE_KEYS.USER_INFO)
|
|
removeStorage(STORAGE_KEYS.NEED_SET_PASSWORD)
|
|
}
|
|
|
|
function setPreferredMiniLoginShopId(shopId) {
|
|
if (shopId === undefined || shopId === null || shopId === '') {
|
|
removeStorage(STORAGE_KEYS.PREFERRED_MINI_LOGIN_SHOP_ID)
|
|
return
|
|
}
|
|
|
|
setStorage(STORAGE_KEYS.PREFERRED_MINI_LOGIN_SHOP_ID, String(shopId))
|
|
}
|
|
|
|
module.exports = {
|
|
getAccessToken,
|
|
getRefreshToken,
|
|
getStoredUserInfo,
|
|
getNeedSetPassword,
|
|
getPreferredMiniLoginShopId,
|
|
isLoggedIn,
|
|
setSession,
|
|
updateStoredUserInfo,
|
|
setNeedSetPassword,
|
|
setPreferredMiniLoginShopId,
|
|
clearSession,
|
|
sanitizeStoredUserInfo
|
|
}
|