提交 d2a5a7af 编写于 作者: C chenruilong

feat: uni-id-co 支持 URL 化

上级 bd689925
## 1.0.29(2022-11-10)
- uni-id-co 支持URL化方式请求 [详情](https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#adapter-http)
## 1.0.28(2022-11-09)
- uni-id-co 升级密码加密算法,支持hmac-sha256加密 [详情](https://uniapp.dcloud.net.cn/uniCloud/uni-id-summary.html#password-safe)
- uni-id-co 新增 开发者可以自定义密码加密规则 [详情](https://uniapp.dcloud.net.cn/uniCloud/uni-id-summary.html#custom-password-encrypt)
- uni-id-co 新增 支持将其他系统用户迁移至uni-id [详情](https://uniapp.dcloud.net.cn/uniCloud/uni-id-summary.html#move-users-to-uni-id)
## 1.0.27(2022-10-26)
- uni-id-co 新增 secureNetworkHandshakeByWeixin 接口,用于建立和微信小程序的安全网络连接
## 1.0.26(2022-10-18)
......
{
"id": "uni-id-pages",
"displayName": "uni-id-pages",
"version": "1.0.28",
"version": "1.0.29",
"description": "云端一体简单、统一、可扩展的用户中心页面模版",
"keywords": [
"用户管理",
......
......@@ -35,7 +35,8 @@ const ERROR = {
UNBIND_NOT_SUPPORTED: 'uni-id-unbind-not-supported',
UNBIND_UNIQUE_LOGIN: 'uni-id-unbind-unique-login',
UNBIND_PASSWORD_NOT_EXISTS: 'uni-id-unbind-password-not-exists',
UNBIND_MOBILE_NOT_EXISTS: 'uni-id-unbind-mobile-not-exists'
UNBIND_MOBILE_NOT_EXISTS: 'uni-id-unbind-mobile-not-exists',
UNSUPPORTED_REQUEST: 'uni-id-unsupported-request'
}
function isUniIdError (errCode) {
......
const { ERROR } = require('./error')
function getHttpClientInfo () {
const requestId = this.getUniCloudRequestId()
const { clientIP, userAgent, source, secretType = 'none' } = this.getClientInfo()
const { clientInfo = {} } = JSON.parse(this.getHttpInfo().body)
return {
...clientInfo,
clientIP,
userAgent,
source,
secretType,
requestId
}
}
function getHttpUniIdToken () {
const { uniIdToken = '' } = JSON.parse(this.getHttpInfo().body)
return uniIdToken
}
function verifyHttpMethod () {
const { headers, httpMethod } = this.getHttpInfo()
if (!/^application\/json/.test(headers['content-type']) || httpMethod.toUpperCase() !== 'POST') {
throw {
errCode: ERROR.UNSUPPORTED_REQUEST,
errMsg: 'unsupported request'
}
}
}
function universal () {
if (this.getClientInfo().source === 'http') {
verifyHttpMethod.call(this)
this.getParams()[0] = JSON.parse(this.getHttpInfo().body).params
this.getUniversalClientInfo = getHttpClientInfo.bind(this)
this.getUniversalUniIdToken = getHttpUniIdToken.bind(this)
} else {
this.getUniversalClientInfo = this.getClientInfo
this.getUniversalUniIdToken = this.getUniIdToken
}
}
module.exports = universal
......@@ -12,6 +12,7 @@ const {
isUniIdError
} = require('./common/error')
const middleware = require('./middleware/index')
const universal = require('./common/universal')
const {
registerAdmin,
......@@ -81,7 +82,10 @@ const {
module.exports = {
async _before () {
const clientInfo = this.getClientInfo()
// 支持 callFunction 与 URL化
universal.call(this)
const clientInfo = this.getUniversalClientInfo()
/**
* 检查clientInfo,无appId和uniPlatform时本云对象无法正常运行
* 此外需要保证用到的clientInfo字段均经过类型检查
......@@ -581,5 +585,5 @@ module.exports = {
/**
* 安全网络握手,目前仅处理微信小程序安全网络握手
*/
secureNetworkHandshakeByWeixin
secureNetworkHandshakeByWeixin
}
......@@ -40,7 +40,8 @@ const sentence = {
'uni-id-unbind-failed': 'Please bind first and then unbind',
'uni-id-unbind-not-supported': 'Unbinding is not supported',
'uni-id-unbind-mobile-not-exists': 'This is the only way to login at the moment, please bind your phone number and then try to unbind',
'uni-id-unbind-password-not-exists': 'Please set a password first'
'uni-id-unbind-password-not-exists': 'Please set a password first',
'uni-id-unsupported-request': 'Unsupported request'
}
module.exports = {
......
......@@ -40,7 +40,8 @@ const sentence = {
'uni-id-unbind-failed': '请先绑定后再解绑',
'uni-id-unbind-not-supported': '不支持解绑',
'uni-id-unbind-mobile-not-exists': '这是当前唯一登录方式,请绑定手机号后再尝试解绑',
'uni-id-unbind-password-not-exists': '请先设置密码在尝试解绑'
'uni-id-unbind-password-not-exists': '请先设置密码在尝试解绑',
'uni-id-unsupported-request': '不支持的请求方式'
}
module.exports = {
......
......@@ -39,7 +39,7 @@ async function getNeedCaptcha ({
const {
data: recentRecord
} = await uniIdLogCollection.where({
ip: this.getClientInfo().clientIP,
ip: this.getUniversalClientInfo().clientIP,
...userIdentifier,
type,
create_date: dbCmd.gt(now - limitDuration)
......@@ -61,7 +61,7 @@ async function verifyCaptcha (params = {}) {
}
}
const payload = await this.uniCaptcha.verify({
deviceId: this.getClientInfo().deviceId,
deviceId: this.getUniversalClientInfo().deviceId,
captcha,
scene
})
......
......@@ -17,7 +17,7 @@ async function realPreLogin (params = {}) {
const {
user
} = params
const appId = this.getClientInfo().appId
const appId = this.getUniversalClientInfo().appId
const userMatched = await findUser({
userQuery: user,
authorizedApp: appId
......@@ -66,7 +66,7 @@ async function preLoginWithPassword (params = {}) {
} = this.config
const {
clientIP
} = this.getClientInfo()
} = this.getUniversalClientInfo()
// 根据ip地址,密码错误次数过多,锁定登录
let loginIPLimit = userRecord.login_ip_limit || []
// 清理无用记录
......@@ -79,7 +79,7 @@ async function preLoginWithPassword (params = {}) {
}
const passwordUtils = new PasswordUtils({
userRecord,
clientInfo: this.getClientInfo(),
clientInfo: this.getUniversalClientInfo(),
passwordSecret: this.config.passwordSecret
})
......@@ -179,7 +179,7 @@ async function postLogin (params = {}) {
const {
clientIP,
uniIdToken
} = this.getClientInfo()
} = this.getUniversalClientInfo()
const uid = user._id
const updateData = {
last_login_date: Date.now(),
......
......@@ -5,11 +5,11 @@ const {
userCollection
} = require('../../common/constants')
async function logout() {
async function logout () {
const {
uniIdToken,
deviceId
} = this.getClientInfo()
} = this.getUniversalClientInfo()
const {
uid
} = await this.uniIdCommon.checkToken(
......@@ -44,4 +44,4 @@ async function logout() {
module.exports = {
logout
}
\ No newline at end of file
}
......@@ -5,7 +5,7 @@ const {
ERROR
} = require('../../common/error')
function getQQPlatform() {
function getQQPlatform () {
const platform = this.clientPlatform
switch (platform) {
case 'app':
......@@ -18,7 +18,7 @@ function getQQPlatform() {
}
}
async function saveQQUserKey({
async function saveQQUserKey ({
openid,
sessionKey, // QQ小程序用户sessionKey
accessToken, // App端QQ用户accessToken
......@@ -26,7 +26,7 @@ async function saveQQUserKey({
} = {}) {
// 微信公众平台、开放平台refreshToken有效期均为30天(微信没有在网络请求里面返回30天这个值,务必注意未来可能出现调整,需及时更新此处逻辑)。
// 此前QQ开放平台有调整过accessToken的过期时间:[access_token有效期由90天缩短至30天](https://wiki.connect.qq.com/%E3%80%90qq%E4%BA%92%E8%81%94%E3%80%91access_token%E6%9C%89%E6%95%88%E6%9C%9F%E8%B0%83%E6%95%B4)
const appId = this.getClientInfo().appId
const appId = this.getUniversalClientInfo().appId
const qqPlatform = getQQPlatform.call(this)
const keyObj = {
dcloudAppid: appId,
......@@ -45,9 +45,9 @@ async function saveQQUserKey({
await this.uniOpenBridge.setUserAccessToken(keyObj, {
access_token: accessToken,
access_token_expired: accessTokenExpired
}, accessTokenExpired ?
Math.floor((accessTokenExpired - Date.now()) / 1000) :
30 * 24 * 60 * 60
}, accessTokenExpired
? Math.floor((accessTokenExpired - Date.now()) / 1000)
: 30 * 24 * 60 * 60
)
break
default:
......@@ -55,7 +55,7 @@ async function saveQQUserKey({
}
}
function generateQQCache({
function generateQQCache ({
sessionKey, // QQ小程序用户sessionKey
accessToken, // App端QQ用户accessToken
accessTokenExpired // App端QQ用户accessToken过期时间
......@@ -84,11 +84,11 @@ function generateQQCache({
}
}
function getQQOpenid({
function getQQOpenid ({
userRecord
} = {}) {
const qqPlatform = getQQPlatform.call(this)
const appId = this.getClientInfo().appId
const appId = this.getUniversalClientInfo().appId
const qqOpenidObj = userRecord.qq_openid
if (!qqOpenidObj) {
return
......@@ -96,7 +96,7 @@ function getQQOpenid({
return qqOpenidObj[`${qqPlatform}_${appId}`] || qqOpenidObj[qqPlatform]
}
async function getQQCacheFallback({
async function getQQCacheFallback ({
userRecord,
key
} = {}) {
......@@ -109,13 +109,13 @@ async function getQQCacheFallback({
return qqCache && qqCache[key]
}
async function getQQCache({
async function getQQCache ({
uid,
userRecord,
key
} = {}) {
const qqPlatform = getQQPlatform.call(this)
const appId = this.getClientInfo().appId
const appId = this.getUniversalClientInfo().appId
if (!userRecord) {
const getUserRes = await userCollection.doc(uid).get()
......
......@@ -24,7 +24,7 @@ async function realPreRegister (params = {}) {
} = params
const userMatched = await findUser({
userQuery: user,
authorizedApp: this.getClientInfo().appId
authorizedApp: this.getUniversalClientInfo().appId
})
if (userMatched.length > 0) {
throw {
......@@ -54,7 +54,7 @@ async function preRegisterWithPassword (params = {}) {
user
})
const passwordUtils = new PasswordUtils({
clientInfo: this.getClientInfo(),
clientInfo: this.getUniversalClientInfo(),
passwordSecret: this.config.passwordSecret
})
const {
......@@ -99,7 +99,7 @@ async function postRegister (params = {}) {
clientIP,
osName,
uniIdToken
} = this.getClientInfo()
} = this.getUniversalClientInfo()
merge(user, extraData)
......@@ -163,7 +163,7 @@ async function postRegister (params = {}) {
if (beforeRegister) {
userRecord = await beforeRegister({
userRecord,
clientInfo: this.getClientInfo()
clientInfo: this.getUniversalClientInfo()
})
}
......
......@@ -29,7 +29,7 @@ async function preBind ({
} = {}) {
const userMatched = await findUser({
userQuery: bindAccount,
authorizedApp: this.getClientInfo().appId
authorizedApp: this.getUniversalClientInfo().appId
})
if (userMatched.length > 0) {
await this.middleware.uniIdLog({
......
......@@ -17,7 +17,7 @@ async function realPreUnifiedLogin (params = {}) {
user,
type
} = params
const appId = this.getClientInfo().appId
const appId = this.getUniversalClientInfo().appId
const userMatched = await findUser({
userQuery: user,
authorizedApp: appId
......
......@@ -13,7 +13,7 @@ async function getPhoneNumber ({
}
return uniCloud.getPhoneNumber({
provider: 'univerify',
appid: this.getClientInfo().appId,
appid: this.getUniversalClientInfo().appId,
apiKey: univerifyConfig.apiKey,
apiSecret: univerifyConfig.apiSecret,
// eslint-disable-next-line camelcase
......
......@@ -23,7 +23,7 @@ async function setVerifyCode ({
scene,
code: code || getVerifyCode(),
state: 0,
ip: this.getClientInfo().clientIP,
ip: this.getUniversalClientInfo().clientIP,
created_date: now,
expired_date: now + expiresIn * 1000
}
......
......@@ -12,7 +12,7 @@ const {
openDataCollection
} = require('../../common/constants')
function decryptWeixinData({
function decryptWeixinData ({
encryptedData,
sessionKey,
iv
......@@ -37,9 +37,9 @@ function decryptWeixinData({
return decoded
}
function getWeixinPlatform() {
function getWeixinPlatform () {
const platform = this.clientPlatform
const userAgent = this.getClientInfo().userAgent
const userAgent = this.getUniversalClientInfo().userAgent
switch (platform) {
case 'app':
case 'app-plus':
......@@ -54,7 +54,7 @@ function getWeixinPlatform() {
}
}
async function saveWeixinUserKey({
async function saveWeixinUserKey ({
openid,
sessionKey, // 微信小程序用户sessionKey
accessToken, // App端微信用户accessToken
......@@ -64,7 +64,7 @@ async function saveWeixinUserKey({
// 微信公众平台、开放平台refreshToken有效期均为30天(微信没有在网络请求里面返回30天这个值,务必注意未来可能出现调整,需及时更新此处逻辑)。
// 此前QQ开放平台有调整过accessToken的过期时间:[access_token有效期由90天缩短至30天](https://wiki.connect.qq.com/%E3%80%90qq%E4%BA%92%E8%81%94%E3%80%91access_token%E6%9C%89%E6%95%88%E6%9C%9F%E8%B0%83%E6%95%B4)
const appId = this.getClientInfo().appId
const appId = this.getUniversalClientInfo().appId
const weixinPlatform = getWeixinPlatform.call(this)
const keyObj = {
dcloudAppid: appId,
......@@ -91,7 +91,7 @@ async function saveWeixinUserKey({
}
}
async function saveSecureNetworkCache({
async function saveSecureNetworkCache ({
code,
openid,
unionid,
......@@ -99,7 +99,7 @@ async function saveSecureNetworkCache({
}) {
const {
appId
} = this.getClientInfo()
} = this.getUniversalClientInfo()
const key = `uni-id:${appId}:weixin-mp:code:${code}:secure-network-cache`
const value = JSON.stringify({
openid,
......@@ -120,7 +120,7 @@ async function saveSecureNetworkCache({
}
}
function generateWeixinCache({
function generateWeixinCache ({
sessionKey, // 微信小程序用户sessionKey
accessToken, // App端微信用户accessToken
refreshToken, // App端微信用户refreshToken
......@@ -153,11 +153,11 @@ function generateWeixinCache({
}
}
function getWeixinOpenid({
function getWeixinOpenid ({
userRecord
} = {}) {
const weixinPlatform = getWeixinPlatform.call(this)
const appId = this.getClientInfo().appId
const appId = this.getUniversalClientInfo().appId
const wxOpenidObj = userRecord.wx_openid
if (!wxOpenidObj) {
return
......@@ -165,7 +165,7 @@ function getWeixinOpenid({
return wxOpenidObj[`${weixinPlatform}_${appId}`] || wxOpenidObj[weixinPlatform]
}
async function getWeixinCacheFallback({
async function getWeixinCacheFallback ({
userRecord,
key
} = {}) {
......@@ -178,13 +178,13 @@ async function getWeixinCacheFallback({
return weixinCache && weixinCache[key]
}
async function getWeixinCache({
async function getWeixinCache ({
uid,
userRecord,
key
} = {}) {
const weixinPlatform = getWeixinPlatform.call(this)
const appId = this.getClientInfo().appId
const appId = this.getUniversalClientInfo().appId
if (!userRecord) {
const getUserRes = await userCollection.doc(uid).get()
userRecord = getUserRes.data[0]
......@@ -212,9 +212,9 @@ async function getWeixinCache({
})
}
async function getWeixinAccessToken() {
async function getWeixinAccessToken () {
const weixinPlatform = getWeixinPlatform.call(this)
const appId = this.getClientInfo().appId
const appId = this.getUniversalClientInfo().appId
const cache = await this.uniOpenBridge.getAccessToken({
dcloudAppid: appId,
......
......@@ -2,7 +2,7 @@ module.exports = async function () {
if (this.authInfo) { // 多次执行auth时如果第一次成功后续不再执行
return
}
const token = this.getUniIdToken()
const token = this.getUniversalUniIdToken()
const payload = await this.uniIdCommon.checkToken(token)
if (payload.errCode) {
throw payload
......
......@@ -19,7 +19,7 @@ module.exports = async function ({
clientIP,
deviceId,
userAgent
} = this.getClientInfo()
} = this.getUniversalClientInfo()
const logData = {
appid: appId,
device_id: deviceId,
......
......@@ -79,7 +79,7 @@ module.exports = async function (params = {}) {
userQuery: {
email
},
authorizedApp: [this.getClientInfo().appId]
authorizedApp: [this.getUniversalClientInfo().appId]
})
if (userMatched.length === 0) {
throw {
......@@ -95,7 +95,7 @@ module.exports = async function (params = {}) {
passwordHash,
version
} = new PasswordUtils({
clientInfo: this.getClientInfo(),
clientInfo: this.getUniversalClientInfo(),
passwordSecret: this.config.passwordSecret
}).generatePasswordHash({
password
......
......@@ -79,7 +79,7 @@ module.exports = async function (params = {}) {
userQuery: {
mobile
},
authorizedApp: [this.getClientInfo().appId]
authorizedApp: [this.getUniversalClientInfo().appId]
})
if (userMatched.length === 0) {
throw {
......@@ -95,7 +95,7 @@ module.exports = async function (params = {}) {
passwordHash,
version
} = new PasswordUtils({
clientInfo: this.getClientInfo(),
clientInfo: this.getUniversalClientInfo(),
passwordSecret: this.config.passwordSecret
}).generatePasswordHash({
password
......
......@@ -33,7 +33,7 @@ module.exports = async function (params = {}) {
} = params
const passwordUtils = new PasswordUtils({
userRecord,
clientInfo: this.getClientInfo(),
clientInfo: this.getUniversalClientInfo(),
passwordSecret: this.config.passwordSecret
})
......
......@@ -83,7 +83,7 @@ module.exports = async function (params = {}) {
}
}
const passwordUtils = new PasswordUtils({
clientInfo: this.getClientInfo(),
clientInfo: this.getUniversalClientInfo(),
passwordSecret: this.config.passwordSecret
})
const {
......
......@@ -107,7 +107,7 @@ module.exports = async function (params = {}) {
if (password) {
const passwordUtils = new PasswordUtils({
clientInfo: this.getClientInfo(),
clientInfo: this.getUniversalClientInfo(),
passwordSecret: this.config.passwordSecret
})
const {
......
......@@ -56,7 +56,7 @@ module.exports = async function (params = {}) {
} = params
const {
appId
} = this.getClientInfo()
} = this.getUniversalClientInfo()
const qqApi = initQQ.call(this)
const qqPlatform = getQQPlatform.call(this)
let apiName
......
......@@ -44,7 +44,7 @@ module.exports = async function (params = {}) {
} = params
const {
appId
} = this.getClientInfo()
} = this.getUniversalClientInfo()
const weixinApi = initWeixin.call(this)
const weixinPlatform = getWeixinPlatform.call(this)
let apiName
......
......@@ -38,7 +38,7 @@ module.exports = async function (params = {}) {
}).limit(1).get()
if (getAdminRes.data.length > 0) {
const [admin] = getAdminRes.data
const appId = this.getClientInfo().appId
const appId = this.getUniversalClientInfo().appId
if (!admin.dcloud_appid || (admin.dcloud_appid && admin.dcloud_appid.includes(appId))) {
return {
......
......@@ -49,7 +49,7 @@ module.exports = async function (params = {}) {
accessTokenExpired
} = params
const qqPlatform = getQQPlatform.call(this)
const appId = this.getClientInfo().appId
const appId = this.getUniversalClientInfo().appId
const qqApi = initQQ.call(this)
const clientPlatform = this.clientPlatform
const apiName = clientPlatform === 'mp-qq' ? 'code2Session' : 'getOpenidByToken'
......
......@@ -34,7 +34,7 @@ module.exports = async function (params = {}) {
code
} = params
const weixinPlatform = getWeixinPlatform.call(this)
const appId = this.getClientInfo().appId
const appId = this.getUniversalClientInfo().appId
const weixinApi = initWeixin.call(this)
const clientPlatform = this.clientPlatform
......
......@@ -16,7 +16,7 @@ const {
*/
module.exports = async function () {
const { uid } = this.authInfo
const { appId } = this.getClientInfo()
const { appId } = this.getUniversalClientInfo()
const qqPlatform = getQQPlatform.call(this)
await preUnBind.call(this, {
......
......@@ -7,7 +7,7 @@ module.exports = async function () {
token,
tokenExpired
} = await this.uniIdCommon.refreshToken({
token: this.getUniIdToken()
token: this.getUniversalUniIdToken()
})
return {
errCode: 0,
......
......@@ -25,7 +25,7 @@ async function setOpendbDevice ({
screenHeight,
romName,
romVersion
} = this.getClientInfo()
} = this.getUniversalClientInfo()
const platform = this.clientPlatform
const now = Date.now()
......@@ -80,7 +80,7 @@ module.exports = async function (params = {}) {
deviceId,
appId,
osName
} = this.getClientInfo()
} = this.getUniversalClientInfo()
let platform = this.clientPlatform
if (platform === 'app') {
platform += osName
......
......@@ -18,7 +18,7 @@ module.exports = async function (params = {}) {
}
this.middleware.validate(params, schema)
const deviceId = this.getClientInfo().deviceId
const deviceId = this.getUniversalClientInfo().deviceId
const {
scene
} = params
......
......@@ -18,7 +18,7 @@ module.exports = async function (params = {}) {
}
this.middleware.validate(params, schema)
const deviceId = this.getClientInfo().deviceId
const deviceId = this.getUniversalClientInfo().deviceId
const {
scene
} = params
......
{
"name": "uni-id-co",
"version": "1.0.28",
"version": "1.0.29",
"description": "",
"main": "index.js",
"keywords": [],
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册