mirror of
https://codeberg.org/timelimit/timelimit-server.git
synced 2026-08-31 19:03:45 +02:00
Remove sign in with Google account
This commit is contained in:
+1
-63
@@ -17,79 +17,17 @@
|
||||
|
||||
import { json } from 'body-parser'
|
||||
import { Router } from 'express'
|
||||
import { OAuth2Client } from 'google-auth-library'
|
||||
import { BadRequest } from 'http-errors'
|
||||
import { Database } from '../database'
|
||||
import { createAuthTokenByMailAddress } from '../function/authentication'
|
||||
import { sendLoginCode, signInByMailCode } from '../function/authentication/login-by-mail'
|
||||
import {
|
||||
isSendMailLoginCodeRequest,
|
||||
isSignInByMailCodeRequest,
|
||||
isSignInWithGoogleRequest
|
||||
isSignInByMailCodeRequest
|
||||
} from './validator'
|
||||
|
||||
const CLIENT_ID = process.env.GOOGLE_SIGN_IN_CLIENT_ID || ''
|
||||
const client = new OAuth2Client(CLIENT_ID)
|
||||
|
||||
const getMailByGoogleAuthToken = async (idToken: string) => {
|
||||
const ticket = await client.verifyIdToken({
|
||||
idToken,
|
||||
audience: CLIENT_ID
|
||||
})
|
||||
|
||||
if (!ticket) {
|
||||
throw new BadRequest()
|
||||
}
|
||||
|
||||
const payload = ticket.getPayload()
|
||||
|
||||
if (!payload) {
|
||||
throw new BadRequest()
|
||||
}
|
||||
|
||||
if (!payload.email_verified) {
|
||||
throw new BadRequest()
|
||||
}
|
||||
|
||||
const mail = payload.email
|
||||
|
||||
if (!mail) {
|
||||
throw new BadRequest()
|
||||
}
|
||||
|
||||
if (!(
|
||||
mail.endsWith('@gmail.com') ||
|
||||
mail.endsWith('@googlemail.com')
|
||||
)) {
|
||||
throw new BadRequest()
|
||||
}
|
||||
|
||||
return mail
|
||||
}
|
||||
|
||||
export const createAuthRouter = (database: Database) => {
|
||||
const router = Router()
|
||||
|
||||
router.post('/sign-in-with-google', json(), async (req, res, next) => {
|
||||
try {
|
||||
if (!isSignInWithGoogleRequest(req.body)) {
|
||||
res.sendStatus(400)
|
||||
return
|
||||
}
|
||||
|
||||
const { googleAuthToken } = req.body
|
||||
|
||||
const mail = await getMailByGoogleAuthToken(googleAuthToken)
|
||||
const mailAuthToken = await createAuthTokenByMailAddress({ mail, database })
|
||||
|
||||
res.json({
|
||||
mailAuthToken
|
||||
})
|
||||
} catch (ex) {
|
||||
next(ex)
|
||||
}
|
||||
})
|
||||
|
||||
router.post('/send-mail-login-code', json(), async (req, res, next) => {
|
||||
try {
|
||||
if (!isSendMailLoginCodeRequest(req.body)) {
|
||||
|
||||
@@ -34,10 +34,6 @@ export interface ClientPullChangesRequest {
|
||||
status: ClientDataStatus
|
||||
}
|
||||
|
||||
export interface SignInWithGoogleRequest {
|
||||
googleAuthToken: string
|
||||
}
|
||||
|
||||
export interface MailAuthTokenRequestBody {
|
||||
mailAuthToken: string
|
||||
}
|
||||
|
||||
+1
-15
@@ -1,5 +1,5 @@
|
||||
// tslint:disable
|
||||
import { ClientPushChangesRequest, ClientPullChangesRequest, SignInWithGoogleRequest, MailAuthTokenRequestBody, CreateFamilyByMailTokenRequest, SignIntoFamilyRequest, RecoverParentPasswordRequest, CanRecoverPasswordRequest, RegisterChildDeviceRequest, SerializedParentAction, SerializedAppLogicAction, SerializedChildAction, CreateRegisterDeviceTokenRequest, CanDoPurchaseRequest, FinishPurchaseByGooglePlayRequest, LinkParentMailAddressRequest, UpdatePrimaryDeviceRequest, RemoveDeviceRequest, RequestWithAuthToken, SendMailLoginCodeRequest, SignInByMailCodeRequest } from './schema'
|
||||
import { ClientPushChangesRequest, ClientPullChangesRequest, MailAuthTokenRequestBody, CreateFamilyByMailTokenRequest, SignIntoFamilyRequest, RecoverParentPasswordRequest, CanRecoverPasswordRequest, RegisterChildDeviceRequest, SerializedParentAction, SerializedAppLogicAction, SerializedChildAction, CreateRegisterDeviceTokenRequest, CanDoPurchaseRequest, FinishPurchaseByGooglePlayRequest, LinkParentMailAddressRequest, UpdatePrimaryDeviceRequest, RemoveDeviceRequest, RequestWithAuthToken, SendMailLoginCodeRequest, SignInByMailCodeRequest } from './schema'
|
||||
const Ajv = require('ajv')
|
||||
const ajv = new Ajv()
|
||||
|
||||
@@ -1369,20 +1369,6 @@ export const isClientPullChangesRequest: (value: object) => value is ClientPullC
|
||||
"definitions": definitions,
|
||||
"$schema": "http://json-schema.org/draft-07/schema#"
|
||||
})
|
||||
export const isSignInWithGoogleRequest: (value: object) => value is SignInWithGoogleRequest = ajv.compile({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"googleAuthToken": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"additionalProperties": false,
|
||||
"required": [
|
||||
"googleAuthToken"
|
||||
],
|
||||
"definitions": definitions,
|
||||
"$schema": "http://json-schema.org/draft-07/schema#"
|
||||
})
|
||||
export const isMailAuthTokenRequestBody: (value: object) => value is MailAuthTokenRequestBody = ajv.compile({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
|
||||
Reference in New Issue
Block a user