Skip to content

Commit

Permalink
Retrieve s2s client credentials from basic auth header (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
byn9826 authored Jul 19, 2024
1 parent 08e882d commit dee1a45
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 24 deletions.
6 changes: 6 additions & 0 deletions server/src/configs/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export type Context = {
Bindings: Bindings;
Variables: {
access_token_body?: typeConfig.AccessTokenBody;
basic_auth_body?: typeConfig.BasicAuthBody;
session: Session;
session_key_rotation: boolean;
};
Expand All @@ -61,6 +62,11 @@ export interface AccessTokenBody {
exp: number;
}

export interface BasicAuthBody {
username: string;
password: string;
}

export interface RefreshTokenBody {
sub: string;
azp: string;
Expand Down
10 changes: 0 additions & 10 deletions server/src/dtos/oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,12 @@ export class PostTokenClientCredentialsReqBodyDto {
@IsEnum(TokenGrantType)
grantType: string

@IsString()
@IsNotEmpty()
clientId: string

@IsString()
@IsNotEmpty()
secret: string

@IsString({ each: true })
@ArrayMinSize(1)
scopes: string[]

constructor (dto: PostTokenClientCredentialsReqBodyDto) {
this.grantType = dto.grantType.toLowerCase()
this.clientId = dto.clientId
this.secret = dto.secret
this.scopes = parseScopes(dto.scopes)
}
}
Expand Down
2 changes: 0 additions & 2 deletions server/src/handlers/postTokenReq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ export const parseClientCredentials = async (c: Context<typeConfig.Context>) =>

const bodyDto = new oauthDto.PostTokenClientCredentialsReqBodyDto({
grantType: String(reqBody.grant_type),
clientId: String(reqBody.client_id),
secret: String(reqBody.client_secret),
scopes: reqBody.scope ? String(reqBody.scope).split(',') : [],
})
await validateUtil.dto(bodyDto)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Context } from 'hono'
import {
Context, Next,
} from 'hono'
import { bearerAuth } from 'hono/bearer-auth'
import { basicAuth } from 'hono/basic-auth'
import { typeConfig } from 'configs'
import { jwtService } from 'services'
import { oauthDto } from 'dtos'

const parseToken = async (
c: Context<typeConfig.Context>, token: string, type: typeConfig.ClientType,
Expand Down Expand Up @@ -83,3 +87,32 @@ export const s2sReadUser = bearerAuth({
return true
},
})

export const s2sBasicAuth = async (
c: Context<typeConfig.Context>, next: Next,
) => {
const reqBody = await c.req.parseBody()
const grantType = String(reqBody.grant_type).toLowerCase()
if (grantType === oauthDto.TokenGrantType.ClientCredentials) {
const authGuard = basicAuth({
verifyUser: (
username, password, c: Context<typeConfig.Context>,
) => {
if (!username || !password) return false
c.set(
'basic_auth_body',
{
username, password,
},
)
return true
},
})
return authGuard(
c,
next,
)
} else {
await next()
}
}
2 changes: 1 addition & 1 deletion server/src/middlewares/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * as setupMiddleware from 'middlewares/setup'
export * as accessTokenMiddleware from 'middlewares/accessToken'
export * as authMiddleware from 'middlewares/auth'
export * as csrfMiddleware from 'middlewares/csrf'
4 changes: 2 additions & 2 deletions server/src/routes/identity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
formatUtil, timeUtil,
} from 'utils'
import {
accessTokenMiddleware, csrfMiddleware,
authMiddleware, csrfMiddleware,
} from 'middlewares'
import {
AuthorizePasswordView, AuthorizeConsentView, AuthorizeAccountView,
Expand Down Expand Up @@ -232,7 +232,7 @@ export const load = (app: typeConfig.App) => {

app.post(
`${BaseRoute}/logout`,
accessTokenMiddleware.spa,
authMiddleware.spa,
async (c) => {
const bodyDto = await logoutReqHandler.parsePost(c)

Expand Down
12 changes: 7 additions & 5 deletions server/src/routes/oauth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import {
cryptoUtil, formatUtil, timeUtil,
} from 'utils'
import { accessTokenMiddleware } from 'middlewares'
import { authMiddleware } from 'middlewares'
import {
getAuthorizeReqHandler, logoutReqHandler, postTokenReqHandler,
} from 'handlers'
Expand Down Expand Up @@ -50,6 +50,7 @@ export const load = (app: typeConfig.App) => {

app.post(
`${BaseRoute}/token`,
authMiddleware.s2sBasicAuth,
async (c) => {
const reqBody = await c.req.parseBody()

Expand Down Expand Up @@ -173,12 +174,13 @@ export const load = (app: typeConfig.App) => {

return c.json(result)
} else if (grantType === oauthDto.TokenGrantType.ClientCredentials) {
const basicAuth = c.get('basic_auth_body')!
const bodyDto = await postTokenReqHandler.parseClientCredentials(c)

const app = await appService.verifyS2SClientRequest(
c,
bodyDto.clientId,
bodyDto.secret,
basicAuth.username,
basicAuth.password,
)

const validScopes = formatUtil.getValidScopes(
Expand All @@ -194,7 +196,7 @@ export const load = (app: typeConfig.App) => {
c,
typeConfig.ClientType.S2S,
currentTimestamp,
bodyDto.clientId,
basicAuth.username,
validScopes.join(' '),
)

Expand Down Expand Up @@ -229,7 +231,7 @@ export const load = (app: typeConfig.App) => {

app.get(
`${BaseRoute}/userinfo`,
accessTokenMiddleware.spaProfile,
authMiddleware.spaProfile,
async (c) => {
const accessTokenBody = c.get('access_token_body')!

Expand Down
6 changes: 3 additions & 3 deletions server/src/routes/user.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import {
routeConfig, typeConfig,
} from 'configs'
import { accessTokenMiddleware } from 'middlewares'
import { authMiddleware } from 'middlewares'
import { userModel } from 'models'

const BaseRoute = routeConfig.InternalRoute.ApiUsers

export const load = (app: typeConfig.App) => {
app.get(
`${BaseRoute}`,
accessTokenMiddleware.s2sReadUser,
authMiddleware.s2sReadUser,
async (c) => {
const users = await userModel.getAll(c.env.DB)
return c.json({ users })
Expand All @@ -18,7 +18,7 @@ export const load = (app: typeConfig.App) => {

app.get(
`${BaseRoute}/:authId`,
accessTokenMiddleware.s2sReadUser,
authMiddleware.s2sReadUser,
async (c) => {
const authId = c.req.param('authId')
const user = await userModel.getByAuthId(
Expand Down

0 comments on commit dee1a45

Please sign in to comment.