Skip to content

Commit

Permalink
login 엔드포인트 다시 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
tirr-c committed Aug 10, 2023
1 parent 9060dcd commit 07267c1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
31 changes: 31 additions & 0 deletions src/api/handlers/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,37 @@ import { ControllableError, AuthorizationError, NoSuchEntryError } from '../../m
import Model from '../../model/model'
import { SignatureError, verifyPubkeyReq } from '../pubkey'

export function login(model: Model): IMiddleware {
return async ctx => {
const session = ctx.state.oidcSession as InstanceType<OIDCProvider['Session']>
const body: any = ctx.request.body

if (!body || typeof body !== 'object') {
ctx.status = 400
return
}

const { username, password } = body

try {
const userIdx = await model.pgDo(tr => model.users.authenticate(tr, username, password))
session.loginAccount({
accountId: String(userIdx),
})
} catch (e) {
if (e instanceof ControllableError) {
ctx.status = 401
} else {
ctx.status = 500
}
return
}

ctx.status = 200
return
}
}

export function loginPAM(model: Model): IMiddleware {
return async ctx => {
const body: any = ctx.request.body
Expand Down
9 changes: 8 additions & 1 deletion src/api/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import bodyParser from 'koa-bodyparser'
import Router from 'koa-router'
import Model from '../model/model'
import Config from '../config'
import { loginPAM, logout, checkLogin, loginLegacy } from './handlers/login'
import { login, loginPAM, logout, checkLogin, loginLegacy } from './handlers/login'
import {
createUser, changePassword, sendChangePasswordEmail,
getUserEmails, getUserInfo, checkChangePasswordEmailToken
Expand Down Expand Up @@ -30,6 +30,13 @@ export function createRouter(model: Model, oidcProvider: OIDCProvider, config: C
return next()
})

/**
* Login API route.
* @param username username.
* @param password password.
* 200 if success, 401 if not.
*/
router.post('/api/login', login(model))
/**
* PAM Login API route.
* @param username username.
Expand Down

0 comments on commit 07267c1

Please sign in to comment.