Skip to content

Commit cb61b0e

Browse files
committed
proper access token generation
1 parent da7dc05 commit cb61b0e

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

sample/oidc.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,18 @@ var claims = {
4343
"role":["Admin","Geek"]
4444
};
4545

46-
var access_token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6ImEzck1VZ01Gdjl0UGNsTGE2eUYzekFrZnF1RSIsImtpZCI6ImEzck1VZ01Gdjl0UGNsTGE2eUYzekFrZnF1RSJ9.eyJpc3MiOiJodHRwczovL2xvY2FsaG9zdDo0NDMzMy9jb3JlIiwiYXVkIjoiaHR0cHM6Ly9sb2NhbGhvc3Q6NDQzMzMvY29yZS9yZXNvdXJjZXMiLCJleHAiOjE0NTkzNzgxNjksIm5iZiI6MTQ1OTM3NDU2OSwiY2xpZW50X2lkIjoianMudG9rZW5tYW5hZ2VyIiwic2NvcGUiOlsib3BlbmlkIiwicHJvZmlsZSIsImVtYWlsIiwicmVhZCIsIndyaXRlIl0sInN1YiI6IjgxODcyNyIsImF1dGhfdGltZSI6MTQ1OTM3NDUyMCwiaWRwIjoiaWRzcnYiLCJhbXIiOlsicGFzc3dvcmQiXX0.Sbr9FcCkx2vMdfCmetF0KzVU-qZlQEXc_Nvp85DSBTX8Cdrh5EpyRn3smN6mzGGq96nDNElIwRsqqEdFImewSMEpPueyXtHEG6rzlIBYvgXo4rhCLroCrDg_DTUCyHoniKHtwz1-MUoxDhef0VVyUWavZ5KmZx-U7-3yN_NlU3CxnDUxq_BLK2IHna3ZldeIhSVFqP9005BUuLsxiyVPV-EJtGaQ8-6VHCLKcmSHqsmwUNzrZoCjdUTm17_YkcLWtAx_dp0vq56mGjwqVREz_ykMMJqBA7Q1S33QHV9L6K_CFYyFLBBhtlzmrgI5QyIBJltd3H5AOKPz1LFKVKToMQ";
47-
var at_hash = "H-4BBL3VfBLNSBcwQuwkuQ";
46+
function genAccessToken(){
47+
return parseInt(Math.random().toString().replace("0.", "")).toString(16);
48+
}
49+
50+
function hashAccessToken(access_token){
51+
var hash = jsrsasign.crypto.Util.hashString(access_token, "sha256")
52+
var left = hash.substr(0, hash.length / 2);
53+
var left_b64u = jsrsasign.hextob64u(left);
54+
return left_b64u;
55+
}
4856

49-
function genIdToken(aud, nonce, isOAuth) {
57+
function genIdToken(aud, nonce, access_token) {
5058
var now = parseInt(Date.now()/1000);
5159
var payload = {
5260
aud : aud,
@@ -60,8 +68,8 @@ function genIdToken(aud, nonce, isOAuth) {
6068
amr : [ "password" ]
6169
};
6270

63-
if (isOAuth) {
64-
payload.at_hash = at_hash;
71+
if (access_token) {
72+
payload.at_hash = hashAccessToken(access_token);
6573
payload.sub = claims.sub;
6674
}
6775
else {
@@ -120,14 +128,15 @@ module.exports = function(baseUrl, app) {
120128
var url = req.query.redirect_uri;
121129

122130
if (isOAuth(response_type)){
131+
var access_token = genAccessToken();
123132
url = addFragment(url, "access_token", access_token);
124133
url = addFragment(url, "token_type", "Bearer");
125134
url = addFragment(url, "expires_in", "3600");
126135
url = addFragment(url, "scope", req.query.scope);
127136
}
128137

129138
if (isOidc(response_type)){
130-
url = addFragment(url, "id_token", genIdToken(req.query.client_id, req.query.nonce, isOAuth(response_type)));
139+
url = addFragment(url, "id_token", genIdToken(req.query.client_id, req.query.nonce, access_token));
131140
url = addFragment(url, "session_state", "123");
132141
}
133142

0 commit comments

Comments
 (0)