firebaseoauth2
Advanced tools
Comparing version
@@ -10,2 +10,169 @@ let FirebaseAdmin | ||
// Token | ||
// 指定したクライアントのAuthorizeCodeを更新する | ||
const updateAuthorizeCode = (uid, client_id) => { | ||
return new Promise(function(resolve, reject){ | ||
if(uid && client_id){ | ||
let authorize_code = nanoid(); | ||
// 認証コードとして記録する | ||
FirebaseDb.ref('/oAuth/AuthorizeCodes/' + authorize_code).set({ | ||
uid : uid, | ||
client_id : client_id | ||
}, function(error){ | ||
// 逆引き用にクライアント情報側にも記録する | ||
/** To-Do : error 処理 **/ | ||
FirebaseDb.ref('/oAuth/users/' + uid + '/' + client_id).update({ | ||
authorize_code : authorize_code | ||
}, function(error){ | ||
/** To-Do : error 処理 **/ | ||
resolve(authorize_code) | ||
}) | ||
return | ||
}) | ||
}else{ | ||
// 必須項目が無い | ||
reject() | ||
} | ||
}) | ||
} | ||
// authorize_code から Token 情報を返す | ||
const getTokensByAuthorizeCode = (authorize_code, client_secret) => { | ||
console.log('getTokensByAuthorizeCode', authorize_code, client_secret) | ||
return new Promise(function(resolve, reject){ | ||
getClientByAuthorizeCode(authorize_code) | ||
.then(function(clientInfo){ | ||
console.log(252, clientInfo) | ||
if(clientInfo.client.client_secret == client_secret){ | ||
updateToken(clientInfo) | ||
.then(function(token){ | ||
resolve(token) | ||
}) | ||
}else{ | ||
// クライアントが認証に失敗した | ||
// invalid_client | ||
reject({ | ||
error : 'invalid_client' | ||
}) | ||
} | ||
}, function(){ | ||
}) | ||
}) | ||
} | ||
const getTokenByRefreshToken = (refresh_token, client_secret) => { | ||
console.log('getTokenByRefreshToken', refresh_token, client_secret) | ||
return new Promise(function(resolve, reject){ | ||
getClientByRefreshToken(refresh_token) | ||
.then(function(clientInfo){ | ||
if(clientInfo.client.client_secret == client_secret){ | ||
updateToken(clientInfo) | ||
.then(function(token){ | ||
console.log(268, token) | ||
resolve(token) | ||
}) | ||
}else{ | ||
// クライアントが認証に失敗した | ||
// invalid_client | ||
reject({ | ||
error : 'invalid_client' | ||
}) | ||
} | ||
}, function(){ | ||
}) | ||
}) | ||
} | ||
const updateToken = (clientInfo) => { | ||
console.log('updateToken', clientInfo) | ||
return new Promise(function(resolve, reject){ | ||
let AccessToken | ||
deleteAccessToken(clientInfo.client.access_token) | ||
.then(function(){ | ||
return deleteRefreshToken(clientInfo.client.refresh_token) | ||
}) | ||
.then(function(){ | ||
return deleteAuthorizeCode(clientInfo.client.authorize_code) | ||
}) | ||
.then(function(){ | ||
return addAccessToken(clientInfo.uid, clientInfo.client_id) | ||
}) | ||
.then(function(_AccessToken){ | ||
AccessToken = _AccessToken | ||
return addRefreshToken(clientInfo.uid, clientInfo.client_id) | ||
}) | ||
.then(function(_RefreshToken){ | ||
console.log(277, AccessToken, _RefreshToken) | ||
const newToken = { | ||
access_token : AccessToken, | ||
refresh_token : _RefreshToken, | ||
expires_in: TOKEN_EXPIRES_IN, | ||
token_type: 'Bearer' | ||
} | ||
FirebaseDb.ref('/oAuth/users/' + clientInfo.uid + '/' + clientInfo.client_id) | ||
.update({ | ||
authorize_code : null, | ||
access_token : newToken.access_token, | ||
refresh_token : newToken.refresh_token, | ||
expires_in: newToken.expires_in, | ||
token_type: newToken.token_type | ||
},function(){ | ||
resolve(newToken) | ||
}) | ||
}) | ||
}) | ||
} | ||
const addAccessToken = (uid, client_id) => { | ||
console.log('addAccessToken', uid, client_id) | ||
return new Promise(function(resolve, reject){ | ||
const newAccessToken = nanoid() | ||
FirebaseDb.ref('/oAuth/tokens/AccessTokens/' + newAccessToken).set({ | ||
uid : uid, | ||
client_id : client_id | ||
},function(error){ | ||
resolve(newAccessToken) | ||
}) | ||
}) | ||
} | ||
const addRefreshToken = (uid, client_id) => { | ||
console.log('addRefreshToken', uid, client_id) | ||
return new Promise(function(resolve, reject){ | ||
const newRefreshToken = nanoid() | ||
FirebaseDb.ref('/oAuth/tokens/RefreshTokens/' + newRefreshToken).set({ | ||
uid : uid, | ||
client_id : client_id | ||
},function(error){ | ||
resolve(newRefreshToken) | ||
}) | ||
}) | ||
} | ||
const getUidtByAccessToken = (access_token) => { | ||
console.log('getUidtByAccessToken', access_token); | ||
return new Promise(function(resolve, reject){ | ||
console.log('getClientByAccessToken', access_token) | ||
const ref = FirebaseDb.ref('/oAuth/tokens/AccessTokens/' + access_token).once('value') | ||
.then(function(snapshot){ | ||
if(snapshot.val()){ | ||
resolve(snapshot.val().uid) | ||
}else{ | ||
reject() | ||
} | ||
},function(){ | ||
reject() | ||
}) | ||
}) | ||
} | ||
// 削除 ======================================================================== | ||
@@ -64,2 +231,33 @@ // Client を削除する | ||
// Client | ||
// authorize_code から Client情報を取得する | ||
const getClientByAuthorizeCode = (authorize_code) => { | ||
console.log('getClientByAuthorizeCode', authorize_code) | ||
const ref = FirebaseDb.ref('/oAuth/AuthorizeCodes/' + authorize_code) | ||
return getClientByRef(ref) | ||
} | ||
// refresh_token から Client情報を取得する | ||
const getClientByRefreshToken = (refresh_token) => { | ||
console.log('getClientByRefreshToken', refresh_token) | ||
const ref = FirebaseDb.ref('/oAuth/tokens/RefreshTokens/' + refresh_token) | ||
return getClientByRef(ref) | ||
} | ||
const getClientByUidAndClientId = (uid, client_id) => { | ||
console.log('getClientByUidAndClientId', uid, client_id) | ||
return new Promise(function(resolve, reject){ | ||
FirebaseDb.ref('/oAuth/users/' + uid + '/' + client_id).once('value') | ||
.then(function(snapshot){ | ||
console.log(408, snapshot.val()) | ||
if(snapshot.val()){ | ||
resolve(snapshot.val()) | ||
}else{ | ||
resolve() | ||
} | ||
}) | ||
}) | ||
} | ||
// Util | ||
const remove = (path) => { | ||
@@ -80,13 +278,30 @@ return new Promise(function(resolve, reject){ | ||
const getClientByUidAndClientId = (uid, client_id) => { | ||
console.log('getClientByUidAndClientId', uid, client_id) | ||
const getClientByRef = (ref) => { | ||
console.log('getClientByRef') | ||
return new Promise(function(resolve, reject){ | ||
FirebaseDb.ref('/oAuth/users/' + uid + '/' + client_id).once('value') | ||
ref.once('value') | ||
.then(function(snapshot){ | ||
console.log(408, snapshot.val()) | ||
if(snapshot.val()){ | ||
resolve(snapshot.val()) | ||
const value = snapshot.val() | ||
const uid = value.uid | ||
const client_id = value.client_id | ||
getClientByUidAndClientId(uid, client_id) | ||
.then(function(client){ | ||
resolve({ | ||
uid : uid, | ||
client_id : client_id, | ||
client : client | ||
}) | ||
}, function(){ | ||
reject() | ||
}) | ||
}else{ | ||
resolve() | ||
console.log(469) | ||
reject() | ||
} | ||
}, function(){ | ||
// error | ||
}) | ||
@@ -97,7 +312,10 @@ }) | ||
const FirebaseOAuth2DB = { | ||
updateAuthorizeCode : updateAuthorizeCode, | ||
clearClient : clearClient, | ||
setFirebaseAdmin : setFirebaseAdmin, | ||
getClientByUidAndClientId : getClientByUidAndClientId | ||
getClientByUidAndClientId : getClientByUidAndClientId, | ||
getTokenByRefreshToken : getTokenByRefreshToken, | ||
getUidtByAccessToken : getUidtByAccessToken | ||
} | ||
module.exports = FirebaseOAuth2DB |
84
index.js
@@ -158,3 +158,3 @@ const express = require("express"); | ||
// authorize_code を作成する | ||
return updateAuthorizeCode(uid, client_id) | ||
return FirebaseOAuth2DB.updateAuthorizeCode(uid, client_id) | ||
}, function(){ | ||
@@ -216,3 +216,3 @@ // Build in ではない | 必須項目が無い | ||
getTokensByAuthorizeCode(authorize_code, client_secret) | ||
FirebaseOAuth2DB.getTokensByAuthorizeCode(authorize_code, client_secret) | ||
.then(function(token){ | ||
@@ -231,3 +231,3 @@ console.log(token); | ||
getTokenByRefreshToken(refresh_token, client_secret) | ||
FirebaseOAuth2DB.getTokenByRefreshToken(refresh_token, client_secret) | ||
.then(function(token){ | ||
@@ -291,30 +291,5 @@ console.log(token); | ||
// 指定したクライアントのAuthorizeCodeを更新する | ||
const updateAuthorizeCode = (uid, client_id) => { | ||
return new Promise(function(resolve, reject){ | ||
if(uid && client_id){ | ||
let authorize_code = nanoid(); | ||
// 認証コードとして記録する | ||
FirebaseDb.ref('/oAuth/AuthorizeCodes/' + authorize_code).set({ | ||
uid : uid, | ||
client_id : client_id | ||
}, function(error){ | ||
// 逆引き用にクライアント情報側にも記録する | ||
/** To-Do : error 処理 **/ | ||
FirebaseDb.ref('/oAuth/users/' + uid + '/' + client_id).update({ | ||
authorize_code : authorize_code | ||
}, function(error){ | ||
/** To-Do : error 処理 **/ | ||
resolve(authorize_code) | ||
}) | ||
return | ||
}) | ||
}else{ | ||
// 必須項目が無い | ||
reject() | ||
} | ||
}) | ||
} | ||
/* | ||
// authorize_code から Token 情報を返す | ||
@@ -345,3 +320,5 @@ const getTokensByAuthorizeCode = (authorize_code, client_secret) => { | ||
} | ||
*/ | ||
/* | ||
const getTokenByRefreshToken = (refresh_token, client_secret) => { | ||
@@ -371,3 +348,5 @@ console.log('getTokenByRefreshToken', refresh_token, client_secret) | ||
} | ||
*/ | ||
/* | ||
const updateToken = (clientInfo) => { | ||
@@ -415,3 +394,5 @@ console.log('updateToken', clientInfo) | ||
} | ||
*/ | ||
/* | ||
const addAccessToken = (uid, client_id) => { | ||
@@ -442,2 +423,3 @@ console.log('addAccessToken', uid, client_id) | ||
} | ||
*/ | ||
@@ -501,2 +483,3 @@ /* | ||
/* | ||
// authorize_code から Client情報を取得する | ||
@@ -508,3 +491,5 @@ const getClientByAuthorizeCode = (authorize_code) => { | ||
} | ||
*/ | ||
/* | ||
// refresh_token から Client情報を取得する | ||
@@ -516,21 +501,7 @@ const getClientByRefreshToken = (refresh_token) => { | ||
} | ||
*/ | ||
const getUidtByAccessToken = (access_token) => { | ||
console.log('getUidtByAccessToken', access_token); | ||
return new Promise(function(resolve, reject){ | ||
console.log('getClientByAccessToken', access_token) | ||
const ref = FirebaseDb.ref('/oAuth/tokens/AccessTokens/' + access_token).once('value') | ||
.then(function(snapshot){ | ||
if(snapshot.val()){ | ||
resolve(snapshot.val().uid) | ||
}else{ | ||
reject() | ||
} | ||
},function(){ | ||
reject() | ||
}) | ||
}) | ||
} | ||
/* | ||
const getClientByRef = (ref) => { | ||
@@ -566,2 +537,3 @@ console.log('getClientByRef') | ||
} | ||
*/ | ||
@@ -585,2 +557,22 @@ /* | ||
/* | ||
const getUidtByAccessToken = (access_token) => { | ||
console.log('getUidtByAccessToken', access_token); | ||
return new Promise(function(resolve, reject){ | ||
console.log('getClientByAccessToken', access_token) | ||
const ref = FirebaseDb.ref('/oAuth/tokens/AccessTokens/' + access_token).once('value') | ||
.then(function(snapshot){ | ||
if(snapshot.val()){ | ||
resolve(snapshot.val().uid) | ||
}else{ | ||
reject() | ||
} | ||
},function(){ | ||
reject() | ||
}) | ||
}) | ||
} | ||
*/ | ||
app.use("/oauth/static", express.static("./oAuth2/public")); | ||
@@ -593,3 +585,3 @@ | ||
return new Promise(function(resolve, reject){ | ||
getUidtByAccessToken(token) | ||
FirebaseOAuth2DB.getUidtByAccessToken(token) | ||
.then(function(uid){ | ||
@@ -596,0 +588,0 @@ console.log(604, uid) |
{ | ||
"name": "firebaseoauth2", | ||
"version": "0.5.2", | ||
"version": "0.5.3", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "indexjs.js", |
25105
28.41%805
30.05%