如何使用GoEasy-OTP?
appkey说明
使用GoEasy-OTP之前,您需要先了解与之相关的appkey, 登录GoEasy控制台,找到“Professional keys”,您会发现3个key:
appkey | 描述 |
---|---|
Client key | 比commonkey更安全的选择。仅用于客户端,必须和OTP一起使用,既可以推送,也可以订阅,服务器端无法使用。 |
Secret key | 用于在生成GoEasy-OTP时,作为加密的秘钥。 |
Restful key | 仅用于调用Restful api,无法用于客户端订阅和推送。 |
生成GoEasy OTP
为了安全,OTP必须在server端生成,生成规则:
- 声明一个字符串,内容为"000"+当前系统毫秒数
- 将Secret key作为秘钥,用AES(ECB)算法对字符串进行加密
- 使用Base64对加密结果进行编码,结果就是GoEasyOTP
- 验证自己的OTP算法是否工作:
验证OTP生成结果
测试参数:
secret key:86726e4356dce2d3
系统毫秒数:1490325990593
测试结果:
GoEasy-otp:+rOKqbTZioistsdMrhon0A==
应用OTP
修改初始化和连接的代码,使用Client key作为appkey,同时传入服务器端生成的OTP
- Javascript
- Android
- iOS
var goEasy = GoEasy.getInstance({
host: "hangzhou.goeasy.io", //若是新加坡区域:singapore.goeasy.io
appkey: "您的client key",
modules: ['pubsub']//根据需要,传入‘pubsub’或'im’,或数组方式同时传入
});
goEasy.connect({
otp: '您服务器端生成的OTP',
onSuccess: function () { //连接成功
console.log("GoEasy connect successfully.") //连接成功
},
onFailed: function (error) { //连接失败
console.log("Failed to connect GoEasy, code:" + error.code + ",error:" + error.content);
},
onProgress: function (attempts) { //连接或自动重连中
console.log("GoEasy is connecting", attempts);
}
});
//初始化GoEasy,新加坡host:singapore.goeasy.io
GoEasy.init(“hangzhou.goeasy.io”, "您的client key", this.getApplicationContext());
//连接GoEasy
val connectOptions = ConnectOptions()
connectOptions.otp = "您服务器端生成的OTP"
val connectEventListener = object : ConnectEventListener() {
override fun onSuccess(data: GResult<*>) {
Log.i("GoEasy","连接成功")
}
override fun onFailed(error: GResult<*>) {
Log.i("GoEasy","连接失败: code: ${error.code} data: ${error.data}")
}
override fun onProgress(attempts: Int) {
Log.i("GoEasy","尝试重连次数:$attempts")
}
}
GoEasy.connect(connectOptions, connectEventListener)
//初始化GoEasy,新加坡host:singapore.goeasy.io
GoEasy.initGoEasy(host: "hangzhou.goeasy.io", appkey: "您的client key")
//连接GoEasy
let connectOptions = ConnectOptions(
otp: "您服务器端生成的OTP"
)
let connectEventListener = ConnectEventListener()
connectEventListener.onSuccess = { result in
print("连接成功.")
}
connectEventListener.onFailed = { result in
print("连接失败result:\(result)")
}
connectEventListener.onProgress = { attempts in
print("尝试重连次数:\(attempts)")
}
GoEasy.connect(options: connectOptions, connectEventListener: connectEventListener)