1 import express from 'express'
8 UnauthorizedClientError,
9 UnsupportedGrantTypeError
10 } from '@node-oauth/oauth2-server'
11 import { randomBytesPromise } from '@server/helpers/core-utils'
12 import { isOTPValid } from '@server/helpers/otp'
13 import { CONFIG } from '@server/initializers/config'
14 import { UserRegistrationModel } from '@server/models/user/user-registration'
15 import { MOAuthClient } from '@server/types/models'
16 import { sha1 } from '@shared/extra-utils'
17 import { HttpStatusCode, ServerErrorCode, UserRegistrationState } from '@shared/models'
18 import { OTP } from '../../initializers/constants'
19 import { BypassLogin, getClient, getRefreshToken, getUser, revokeToken, saveToken } from './oauth-model'
21 class MissingTwoFactorError extends Error {
22 code = HttpStatusCode.UNAUTHORIZED_401
23 name = ServerErrorCode.MISSING_TWO_FACTOR
26 class InvalidTwoFactorError extends Error {
27 code = HttpStatusCode.BAD_REQUEST_400
28 name = ServerErrorCode.INVALID_TWO_FACTOR
31 class RegistrationWaitingForApproval extends Error {
32 code = HttpStatusCode.BAD_REQUEST_400
33 name = ServerErrorCode.ACCOUNT_WAITING_FOR_APPROVAL
36 class RegistrationApprovalRejected extends Error {
37 code = HttpStatusCode.BAD_REQUEST_400
38 name = ServerErrorCode.ACCOUNT_APPROVAL_REJECTED
43 * Reimplement some functions of OAuth2Server to inject external auth methods
46 const oAuthServer = new OAuth2Server({
48 accessTokenLifetime: CONFIG.OAUTH2.TOKEN_LIFETIME.ACCESS_TOKEN / 1000,
49 refreshTokenLifetime: CONFIG.OAUTH2.TOKEN_LIFETIME.REFRESH_TOKEN / 1000,
51 // See https://github.com/oauthjs/node-oauth2-server/wiki/Model-specification for the model specifications
52 model: require('./oauth-model')
55 // ---------------------------------------------------------------------------
57 async function handleOAuthToken (req: express.Request, options: { refreshTokenAuthName?: string, bypassLogin?: BypassLogin }) {
58 const request = new Request(req)
59 const { refreshTokenAuthName, bypassLogin } = options
61 if (request.method !== 'POST') {
62 throw new InvalidRequestError('Invalid request: method must be POST')
65 if (!request.is([ 'application/x-www-form-urlencoded' ])) {
66 throw new InvalidRequestError('Invalid request: content must be application/x-www-form-urlencoded')
69 const clientId = request.body.client_id
70 const clientSecret = request.body.client_secret
72 if (!clientId || !clientSecret) {
73 throw new InvalidClientError('Invalid client: cannot retrieve client credentials')
76 const client = await getClient(clientId, clientSecret)
78 throw new InvalidClientError('Invalid client: client is invalid')
81 const grantType = request.body.grant_type
83 throw new InvalidRequestError('Missing parameter: `grant_type`')
86 if (![ 'password', 'refresh_token' ].includes(grantType)) {
87 throw new UnsupportedGrantTypeError('Unsupported grant type: `grant_type` is invalid')
90 if (!client.grants.includes(grantType)) {
91 throw new UnauthorizedClientError('Unauthorized client: `grant_type` is invalid')
94 if (grantType === 'password') {
95 return handlePasswordGrant({
102 return handleRefreshGrant({
109 function handleOAuthAuthenticate (
110 req: express.Request,
111 res: express.Response
113 return oAuthServer.authenticate(new Request(req), new Response(res))
117 MissingTwoFactorError,
118 InvalidTwoFactorError,
121 handleOAuthAuthenticate
124 // ---------------------------------------------------------------------------
126 async function handlePasswordGrant (options: {
129 bypassLogin?: BypassLogin
131 const { request, client, bypassLogin } = options
133 if (!request.body.username) {
134 throw new InvalidRequestError('Missing parameter: `username`')
137 if (!bypassLogin && !request.body.password) {
138 throw new InvalidRequestError('Missing parameter: `password`')
141 const user = await getUser(request.body.username, request.body.password, bypassLogin)
143 const registration = await UserRegistrationModel.loadByEmailOrUsername(request.body.username)
145 if (registration?.state === UserRegistrationState.REJECTED) {
146 throw new RegistrationApprovalRejected('Registration approval for this account has been rejected')
147 } else if (registration?.state === UserRegistrationState.PENDING) {
148 throw new RegistrationWaitingForApproval('Registration for this account is awaiting approval')
151 throw new InvalidGrantError('Invalid grant: user credentials are invalid')
154 if (user.otpSecret) {
155 if (!request.headers[OTP.HEADER_NAME]) {
156 throw new MissingTwoFactorError('Missing two factor header')
159 if (await isOTPValid({ encryptedSecret: user.otpSecret, token: request.headers[OTP.HEADER_NAME] }) !== true) {
160 throw new InvalidTwoFactorError('Invalid two factor header')
164 const token = await buildToken()
166 return saveToken(token, client, user, { bypassLogin })
169 async function handleRefreshGrant (options: {
172 refreshTokenAuthName: string
174 const { request, client, refreshTokenAuthName } = options
176 if (!request.body.refresh_token) {
177 throw new InvalidRequestError('Missing parameter: `refresh_token`')
180 const refreshToken = await getRefreshToken(request.body.refresh_token)
183 throw new InvalidGrantError('Invalid grant: refresh token is invalid')
186 if (refreshToken.client.id !== client.id) {
187 throw new InvalidGrantError('Invalid grant: refresh token is invalid')
190 if (refreshToken.refreshTokenExpiresAt && refreshToken.refreshTokenExpiresAt < new Date()) {
191 throw new InvalidGrantError('Invalid grant: refresh token has expired')
194 await revokeToken({ refreshToken: refreshToken.refreshToken })
196 const token = await buildToken()
198 return saveToken(token, client, refreshToken.user, { refreshTokenAuthName })
201 function generateRandomToken () {
202 return randomBytesPromise(256)
203 .then(buffer => sha1(buffer))
206 function getTokenExpiresAt (type: 'access' | 'refresh') {
207 const lifetime = type === 'access'
208 ? CONFIG.OAUTH2.TOKEN_LIFETIME.ACCESS_TOKEN
209 : CONFIG.OAUTH2.TOKEN_LIFETIME.REFRESH_TOKEN
211 return new Date(Date.now() + lifetime)
214 async function buildToken () {
215 const [ accessToken, refreshToken ] = await Promise.all([ generateRandomToken(), generateRandomToken() ])
220 accessTokenExpiresAt: getTokenExpiresAt('access'),
221 refreshTokenExpiresAt: getTokenExpiresAt('refresh')