2021-04-01 06:03:34 +00:00
|
|
|
const {OAuth2Client} = require('google-auth-library');
|
2021-04-03 05:14:34 +00:00
|
|
|
module.exports = (s,config,lang) => {
|
2021-04-03 21:16:15 +00:00
|
|
|
const {
|
|
|
|
basicAuth,
|
|
|
|
} = require('./utils.js')(s,config,lang)
|
2021-04-01 06:03:34 +00:00
|
|
|
const client = new OAuth2Client(config.appTokenGoogle);
|
|
|
|
async function verifyToken(userLoginToken) {
|
|
|
|
const ticket = await client.verifyIdToken({
|
|
|
|
idToken: userLoginToken,
|
|
|
|
audience: config.appTokenGoogle,
|
|
|
|
});
|
|
|
|
const payload = ticket.getPayload();
|
|
|
|
const userid = payload['sub'];
|
|
|
|
return {
|
|
|
|
ok: !!payload.email,
|
|
|
|
user: payload.email ? {
|
|
|
|
id: userid,
|
|
|
|
name: payload.name,
|
|
|
|
email: payload.email,
|
|
|
|
picture: payload.picture,
|
|
|
|
} : null,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async function bindLoginIdToUser(loginId,groupKey,userId) {
|
|
|
|
const response = {ok: false}
|
|
|
|
const searchResponse = await s.knexQueryPromise({
|
|
|
|
action: "select",
|
|
|
|
columns: '*',
|
|
|
|
table: "LoginTokens",
|
|
|
|
where: [
|
2021-04-03 21:27:08 +00:00
|
|
|
['loginId','=',`google-${loginId}`],
|
2021-04-01 06:03:34 +00:00
|
|
|
]
|
|
|
|
})
|
|
|
|
if(!searchResponse.rows[0]){
|
|
|
|
const insertResponse = await s.knexQueryPromise({
|
|
|
|
action: "insert",
|
|
|
|
table: "LoginTokens",
|
|
|
|
insert: {
|
2021-04-03 21:27:08 +00:00
|
|
|
loginId: `google-${loginId}`,
|
2021-04-01 06:03:34 +00:00
|
|
|
ke: groupKey,
|
|
|
|
uid: userId,
|
|
|
|
lastLogin: new Date(),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
response.ok = insertResponse.ok
|
|
|
|
}
|
|
|
|
return response
|
|
|
|
}
|
|
|
|
async function refreshLoginTokenAccessDate(loginId) {
|
|
|
|
const response = {ok: false}
|
|
|
|
const updateResponse = await s.knexQueryPromise({
|
|
|
|
action: "update",
|
|
|
|
table: "LoginTokens",
|
|
|
|
update: {
|
2021-04-03 21:27:08 +00:00
|
|
|
lastLogin: new Date()
|
2021-04-01 06:03:34 +00:00
|
|
|
},
|
|
|
|
where: [
|
2021-04-03 21:27:08 +00:00
|
|
|
['loginId','=',`google-${loginId}`],
|
2021-04-01 06:03:34 +00:00
|
|
|
]
|
|
|
|
})
|
|
|
|
response.ok = updateResponse.ok
|
|
|
|
return response
|
|
|
|
}
|
|
|
|
async function deleteLoginToken(loginId) {
|
|
|
|
const response = {ok: false}
|
|
|
|
const updateResponse = await s.knexQueryPromise({
|
|
|
|
action: "delete",
|
|
|
|
table: "LoginTokens",
|
|
|
|
where: [
|
2021-04-03 21:27:08 +00:00
|
|
|
['loginId','=',`google-${loginId}`],
|
2021-04-01 06:03:34 +00:00
|
|
|
]
|
|
|
|
})
|
|
|
|
response.ok = updateResponse.ok
|
|
|
|
return response
|
|
|
|
}
|
|
|
|
async function loginWithGoogleAccount(userLoginToken) {
|
2021-04-03 21:16:15 +00:00
|
|
|
const response = {ok: false, googleSignedIn: false}
|
2021-04-01 06:03:34 +00:00
|
|
|
const tokenResponse = await verifyToken(userLoginToken)
|
|
|
|
if(tokenResponse.ok){
|
|
|
|
const user = tokenResponse.user
|
2021-04-03 21:16:15 +00:00
|
|
|
response.googleSignedIn = true
|
|
|
|
response.googleUser = user
|
2021-04-01 06:03:34 +00:00
|
|
|
const searchResponse = await s.knexQueryPromise({
|
|
|
|
action: "select",
|
|
|
|
columns: '*',
|
|
|
|
table: "LoginTokens",
|
|
|
|
where: [
|
2021-04-03 21:27:08 +00:00
|
|
|
['loginId','=',`google-${user.id}`],
|
2021-04-01 06:03:34 +00:00
|
|
|
]
|
|
|
|
})
|
2021-04-03 05:14:34 +00:00
|
|
|
if(searchResponse.rows[0]){
|
|
|
|
const loginTokenRow = searchResponse.rows[0]
|
2021-04-01 06:03:34 +00:00
|
|
|
const userResponse = await s.knexQueryPromise({
|
|
|
|
action: "select",
|
|
|
|
columns: '*',
|
|
|
|
table: "Users",
|
|
|
|
where: [
|
|
|
|
['uid','=',loginTokenRow.uid],
|
|
|
|
['ke','=',loginTokenRow.ke],
|
|
|
|
]
|
|
|
|
})
|
|
|
|
response.ok = true
|
2021-04-03 05:14:34 +00:00
|
|
|
userResponse.rows[0].details = s.parseJSON(userResponse.rows[0].details)
|
|
|
|
response.user = userResponse.rows[0]
|
2021-04-01 06:03:34 +00:00
|
|
|
}else{
|
2021-04-03 05:14:34 +00:00
|
|
|
console.log('This Token is Not Binded to a User!')
|
2021-04-01 06:03:34 +00:00
|
|
|
// make new if no users?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return response
|
|
|
|
}
|
2021-04-03 21:16:15 +00:00
|
|
|
s.onProcessReady(() => {
|
|
|
|
s.alternateLogins['google'] = async (params) => {
|
|
|
|
const response = { ok: false }
|
|
|
|
const loginToken = params.alternateLoginToken
|
|
|
|
const username = params.mail
|
|
|
|
const password = params.pass
|
|
|
|
const googleLoginResponse = await loginWithGoogleAccount(loginToken)
|
|
|
|
if(googleLoginResponse.user){
|
|
|
|
response.ok = true
|
|
|
|
response.user = googleLoginResponse.user
|
|
|
|
}else if(config.allowBindingAltLoginsFromLoginPage && googleLoginResponse.googleSignedIn && username && password){
|
|
|
|
const basicAuthResponse = await basicAuth(username,password)
|
|
|
|
if(basicAuthResponse.user){
|
|
|
|
const user = basicAuthResponse.user
|
|
|
|
const loginId = googleLoginResponse.googleUser.id
|
|
|
|
const groupKey = user.ke
|
|
|
|
const userId = user.uid
|
|
|
|
const bindResponse = await bindLoginIdToUser(loginId,groupKey,userId)
|
|
|
|
response.ok = true
|
|
|
|
response.user = basicAuthResponse.user
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return response
|
2021-04-03 05:14:34 +00:00
|
|
|
}
|
2021-04-03 21:16:15 +00:00
|
|
|
// s.customAutoLoadTree['LibsJs'].push(`dash2.googleSignIn.js`)
|
|
|
|
})
|
2021-04-01 06:03:34 +00:00
|
|
|
return {
|
|
|
|
client: client,
|
|
|
|
verifyToken: verifyToken,
|
|
|
|
deleteLoginToken: deleteLoginToken,
|
|
|
|
bindLoginIdToUser: bindLoginIdToUser,
|
|
|
|
loginWithGoogleAccount: loginWithGoogleAccount,
|
|
|
|
refreshLoginTokenAccessDate: refreshLoginTokenAccessDate,
|
|
|
|
}
|
|
|
|
}
|