Skip to content

Commit 2b2d65c

Browse files
author
dfounderliu
committed
add auth
1 parent c1afcc3 commit 2b2d65c

File tree

12 files changed

+102
-26
lines changed

12 files changed

+102
-26
lines changed

deploy/tencentDeploy.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,19 @@ class TencentDeploy {
2626
}
2727

2828
async deploy() {
29+
const provider = new tencentProvider(this.serverless, this.options)
2930
if (!this.options.credentials || !this.options.credentials.tencent_secret_id) {
30-
const provider = new tencentProvider(this.serverless, this.options)
3131
const tencentTemp = await provider.getTempKey()
3232
this.options.credentials = {
3333
tencent_secret_id: tencentTemp.tencent_secret_id,
3434
tencent_secret_key: tencentTemp.tencent_secret_key,
35-
tencent_appid: tencentTemp.tencent_appid
35+
tencent_appid: tencentTemp.tencent_appid,
36+
tencent_owneruin: tencentTemp.tencent_owneruin
3637
}
3738
this.options.token = tencentTemp.token
3839
this.options.timestamp = tencentTemp.timestamp
3940
}
41+
await provider.getUserAuth(this.options.credentials.tencent_owneruin)
4042

4143
const services = this.provider.getServiceResource()
4244
const func = new DeployFunction(this.options, this.serverless)

deploy/tencentDeployFunction.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,19 @@ class TencentDeployFunction {
3131
}
3232

3333
async deploy() {
34+
const provider = new tencentProvider(this.serverless, this.options)
3435
if (!this.options.credentials || !this.options.credentials.tencent_secret_id) {
35-
const provider = new tencentProvider(this.serverless, this.options)
3636
const tencentTemp = await provider.getTempKey()
3737
this.options.credentials = {
3838
tencent_secret_id: tencentTemp.tencent_secret_id,
3939
tencent_secret_key: tencentTemp.tencent_secret_key,
40-
tencent_appid: tencentTemp.tencent_appid
40+
tencent_appid: tencentTemp.tencent_appid,
41+
tencent_owneruin: tencentTemp.tencent_owneruin
4142
}
4243
this.options.token = tencentTemp.token
4344
this.options.timestamp = tencentTemp.timestamp
4445
}
46+
await provider.getUserAuth(this.options.credentials.tencent_owneruin)
4547

4648
const services = this.provider.getServiceResource()
4749

deploy/tencentDeployList.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,19 @@ class TencentDeployList {
2323
}
2424

2525
async serviceList() {
26+
const provider = new tencentProvider(this.serverless, this.options)
2627
if (!this.options.credentials || !this.options.credentials.tencent_secret_id) {
27-
const provider = new tencentProvider(this.serverless, this.options)
2828
const tencentTemp = await provider.getTempKey()
2929
this.options.credentials = {
3030
tencent_secret_id: tencentTemp.tencent_secret_id,
3131
tencent_secret_key: tencentTemp.tencent_secret_key,
32-
tencent_appid: tencentTemp.tencent_appid
32+
tencent_appid: tencentTemp.tencent_appid,
33+
tencent_owneruin: tencentTemp.tencent_owneruin
3334
}
3435
this.options.token = tencentTemp.token
3536
this.options.timestamp = tencentTemp.timestamp
3637
}
38+
await provider.getUserAuth(this.options.credentials.tencent_owneruin)
3739
const Handler = new RollbackService(this.options, this.serverless)
3840
const fileKeyPrefix = this.serverless.service.service + '-' + this.options.stage
3941
const cosBucket = this.provider.getDeployCosBucket()

deploy/tencentDeployListFunctions.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,19 @@ class TencentDeployListFunction {
2525
}
2626

2727
async functionList() {
28+
const provider = new tencentProvider(this.serverless, this.options)
2829
if (!this.options.credentials || !this.options.credentials.tencent_secret_id) {
29-
const provider = new tencentProvider(this.serverless, this.options)
3030
const tencentTemp = await provider.getTempKey()
3131
this.options.credentials = {
3232
tencent_secret_id: tencentTemp.tencent_secret_id,
3333
tencent_secret_key: tencentTemp.tencent_secret_key,
34-
tencent_appid: tencentTemp.tencent_appid
34+
tencent_appid: tencentTemp.tencent_appid,
35+
tencent_owneruin: tencentTemp.tencent_owneruin
3536
}
3637
this.options.token = tencentTemp.token
3738
this.options.timestamp = tencentTemp.timestamp
3839
}
40+
await provider.getUserAuth(this.options.credentials.tencent_owneruin)
3941
try {
4042
const infoHandler = new InfoFunction(this.options, this.serverless)
4143
const deployHandler = new ListFunctions(this.options, this.serverless)

info/tencentInfo.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,19 @@ class TencentInfo {
2424
}
2525

2626
async info() {
27+
const provider = new tencentProvider(this.serverless, this.options)
2728
if (!this.options.credentials || !this.options.credentials.tencent_secret_id) {
28-
const provider = new tencentProvider(this.serverless, this.options)
2929
const tencentTemp = await provider.getTempKey()
3030
this.options.credentials = {
3131
tencent_secret_id: tencentTemp.tencent_secret_id,
3232
tencent_secret_key: tencentTemp.tencent_secret_key,
33-
tencent_appid: tencentTemp.tencent_appid
33+
tencent_appid: tencentTemp.tencent_appid,
34+
tencent_owneruin: tencentTemp.tencent_owneruin
3435
}
3536
this.options.token = tencentTemp.token
3637
this.options.timestamp = tencentTemp.timestamp
3738
}
39+
await provider.getUserAuth(this.options.credentials.tencent_owneruin)
3840
try {
3941
const region = this.options.region
4042
const handler = new InfoFunction(this.options, this.serverless)

invoke/tencentInvoke.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,19 @@ class TencentInvoke {
2323
}
2424

2525
async invoke() {
26+
const provider = new tencentProvider(this.serverless, this.options)
2627
if (!this.options.credentials || !this.options.credentials.tencent_secret_id) {
27-
const provider = new tencentProvider(this.serverless, this.options)
2828
const tencentTemp = await provider.getTempKey()
2929
this.options.credentials = {
3030
tencent_secret_id: tencentTemp.tencent_secret_id,
3131
tencent_secret_key: tencentTemp.tencent_secret_key,
32-
tencent_appid: tencentTemp.tencent_appid
32+
tencent_appid: tencentTemp.tencent_appid,
33+
tencent_owneruin: tencentTemp.tencent_owneruin
3334
}
3435
this.options.token = tencentTemp.token
3536
this.options.timestamp = tencentTemp.timestamp
3637
}
38+
await provider.getUserAuth(this.options.credentials.tencent_owneruin)
3739
try {
3840
const options = {
3941
region: this.options.region

logs/tencentLogs.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,19 @@ class TencentLogs {
4949
}
5050

5151
async logs() {
52+
const provider = new tencentProvider(this.serverless, this.options)
5253
if (!this.options.credentials || !this.options.credentials.tencent_secret_id) {
53-
const provider = new tencentProvider(this.serverless, this.options)
5454
const tencentTemp = await provider.getTempKey()
5555
this.options.credentials = {
5656
tencent_secret_id: tencentTemp.tencent_secret_id,
5757
tencent_secret_key: tencentTemp.tencent_secret_key,
58-
tencent_appid: tencentTemp.tencent_appid
58+
tencent_appid: tencentTemp.tencent_appid,
59+
tencent_owneruin: tencentTemp.tencent_owneruin
5960
}
6061
this.options.token = tencentTemp.token
6162
this.options.timestamp = tencentTemp.timestamp
6263
}
64+
await provider.getUserAuth(this.options.credentials.tencent_owneruin)
6365
try {
6466
const timeFormat = 'yyyy-MM-dd hh:mm:ss'
6567
this.serverless.cli.log(`Get function logs...`)

metrics/tencentMetrics.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,19 @@ class TencentInfo {
5757
}
5858

5959
async metrics() {
60+
const provider = new tencentProvider(this.serverless, this.options)
6061
if (!this.options.credentials || !this.options.credentials.tencent_secret_id) {
61-
const provider = new tencentProvider(this.serverless, this.options)
6262
const tencentTemp = await provider.getTempKey()
6363
this.options.credentials = {
6464
tencent_secret_id: tencentTemp.tencent_secret_id,
6565
tencent_secret_key: tencentTemp.tencent_secret_key,
66-
tencent_appid: tencentTemp.tencent_appid
66+
tencent_appid: tencentTemp.tencent_appid,
67+
tencent_owneruin: tencentTemp.tencent_owneruin
6768
}
6869
this.options.token = tencentTemp.token
6970
this.options.timestamp = tencentTemp.timestamp
7071
}
72+
await provider.getUserAuth(this.options.credentials.tencent_owneruin)
7173
try {
7274
const Handler = new MetricsFunction(this.options, this.serverless)
7375
const functionList = await Handler.functionList(

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
"request": "^2.88.0",
4444
"tencentcloud-sdk-nodejs": "^3.0.87",
4545
"universal-analytics": "^0.4.20",
46-
"tencent-login": "^0.1.6"
46+
"tencent-login": "^0.1.6",
47+
"serverless-tencent-auth-tool": "^1.0.0"
4748
},
4849
"devDependencies": {
4950
"coveralls": "^3.0.5",

provider/tencentProvider.js

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ const os = require('os')
44
const ini = require('ini')
55
const _ = require('lodash')
66
const util = require('util')
7+
const QRCode = require('qrcode')
78
const TencentLogin = require('tencent-login')
89
const tencentcloud = require('tencentcloud-sdk-nodejs')
10+
const { GetUserAuthInfo } = require('serverless-tencent-tools').Account
911
const ClientProfile = require('tencentcloud-sdk-nodejs/tencentcloud/common/profile/client_profile.js')
1012
const HttpProfile = require('tencentcloud-sdk-nodejs/tencentcloud/common/profile/http_profile.js')
1113
const AbstractModel = require('tencentcloud-sdk-nodejs/tencentcloud/common/abstract_model')
@@ -17,7 +19,6 @@ const constants = {
1719
class GetUserAppIdResponse extends AbstractModel {
1820
constructor() {
1921
super()
20-
2122
this.RequestId = null
2223
}
2324

@@ -26,6 +27,7 @@ class GetUserAppIdResponse extends AbstractModel {
2627
return
2728
}
2829
this.AppId = 'RequestId' in params ? params.AppId : null
30+
this.OwnerUin = 'RequestId' in params ? params.OwnerUin : null
2931
this.RequestId = 'RequestId' in params ? params.RequestId : null
3032
}
3133
}
@@ -54,6 +56,36 @@ class TencentProvider {
5456
return constants.providerName
5557
}
5658

59+
async getUserAuth(uin) {
60+
try {
61+
const getUserAuthInfo = new GetUserAuthInfo()
62+
const result = await getUserAuthInfo.isAuth(uin)
63+
if (result['Error'] == true) {
64+
console.log('Failed to get real name authentication result.')
65+
process.exit(-1)
66+
} else {
67+
if (result['Message']['Authentication'] == 1) {
68+
return true
69+
}
70+
const verifyUrl = 'https://cloud.tencent.com/verify/identity'
71+
console.log(
72+
"You don't have real name authentication yet. You can open the url or scan QR code for real name authentication."
73+
)
74+
console.log('Real name authentication url: ')
75+
console.log(verifyUrl)
76+
console.log('Real name authentication QR code: ')
77+
QRCode.toString(verifyUrl, { type: 'terminal' }, function(err, url) {
78+
console.log(url)
79+
})
80+
console.log('Please re operate after real name authentication.')
81+
process.exit(-1)
82+
}
83+
} catch (e) {
84+
console.log(e)
85+
process.exit(-1)
86+
}
87+
}
88+
5789
getAppid(credentials) {
5890
const secret_id = credentials.SecretId
5991
const secret_key = credentials.SecretKey
@@ -137,6 +169,14 @@ class TencentProvider {
137169
uuid: tencent_credentials.uuid,
138170
timestamp: tencent_credentials.timestamp
139171
}
172+
// From cam to getting appid
173+
const userInfo = await this.getAppid({
174+
SecretId: tencent.tencent_secret_id,
175+
SecretKey: tencent.tencent_secret_key,
176+
token: tencent.token
177+
})
178+
tencent.tencent_owneruin = userInfo.OwnerUin
179+
tencent.tencent_appid = userInfo.AppId
140180
await fs.writeFileSync('./.env_temp', JSON.stringify(tencent))
141181
return tencent
142182
} catch (e) {
@@ -152,10 +192,16 @@ class TencentProvider {
152192
try {
153193
const tencent = {}
154194
const tencent_credentials_read = JSON.parse(data)
155-
if (
156-
Date.now() / 1000 - tencent_credentials_read.timestamp <= 6000 &&
157-
tencent_credentials_read.tencent_appid
158-
) {
195+
if (Date.now() / 1000 - tencent_credentials_read.timestamp <= 6000) {
196+
const userInfo = await this.getAppid({
197+
SecretId: tencent_credentials_read.tencent_secret_id,
198+
SecretKey: tencent_credentials_read.tencent_secret_key,
199+
token: tencent_credentials_read.token
200+
})
201+
202+
// From cam to getting appid
203+
tencent_credentials_read.tencent_owneruin = userInfo.OwnerUin
204+
tencent_credentials_read.tencent_appid = userInfo.AppId
159205
return tencent_credentials_read
160206
}
161207
const login = new TencentLogin()
@@ -175,6 +221,14 @@ class TencentProvider {
175221
tencent.uuid = tencent_credentials_read.uuid
176222
tencent.timestamp = Date.now() / 1000
177223
await fs.writeFileSync('./.env_temp', JSON.stringify(tencent))
224+
// From cam to getting appid
225+
const userInfo = await this.getAppid({
226+
SecretId: tencent.tencent_secret_id,
227+
SecretKey: tencent.tencent_secret_key,
228+
token: tencent.token
229+
})
230+
tencent.tencent_owneruin = userInfo.OwnerUin
231+
tencent.tencent_appid = userInfo.AppId
178232
return tencent
179233
}
180234
return await that.doLogin()
@@ -242,6 +296,7 @@ class TencentProvider {
242296
SecretKey: this.options.credentials.tencent_secret_key
243297
})
244298
this.options.credentials.tencent_appid = appid.AppId
299+
this.options.credentials.tencent_owneruin = appid.OwnerUin
245300
}
246301
} catch (e) {}
247302
return

0 commit comments

Comments
 (0)