Skip to content

Commit

Permalink
Finish basic authentication flow
Browse files Browse the repository at this point in the history
  • Loading branch information
noo-zh committed Oct 24, 2018
1 parent 937bc13 commit 045f9ae
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,67 @@ class Ukey1 {

return await this.makeRequest(endpoint, method, headers, body);
}

isValidSignature(signature, params) {
if (!signature) {
throw new Error('No signature');
}

if (!params) {
params = {};
}

let requestId, connectId, result, code;
requestId = params.requestId || '';
connectId = params.connectId || '';
result = params.result || '';
code = params.code || '';
const str = this.appId + requestId + connectId + code + result;
const verifier = crypto.createVerify('sha512');
verifier.update(str);

return verifier.verify(this.secretKey, signature, 'base64');
}

async getAccessToken(options) {
const endpoint = '/auth/v2/token';
const method = 'POST';
let requestId, connectId, code, headers, body;

if (!options) {
options = {};
}

requestId = options.requestId || '';
connectId = options.connectId || '';
code = options.code || '';

if (!(requestId && connectId && code)) {
throw new Error('Unspecified input params');
}

body = this.prepareBody({
request_id: requestId,
connect_id: connectId,
auth_code: code
});
headers = this.prepareHeaders(this.prepareSignature(endpoint, method, body), null);

return await this.makeRequest(endpoint, method, headers, body);
}

async getUserData(accessToken) {
const endpoint = '/auth/v2/me';
const method = 'GET';

if (!accessToken) {
throw new Error('Unspecified access token');
}

const headers = this.prepareHeaders(this.prepareSignature(endpoint, method, null, accessToken), accessToken);

return await this.makeRequest(endpoint, method, headers);
}
}

module.exports = Ukey1;

0 comments on commit 045f9ae

Please sign in to comment.