Skip to content

Commit

Permalink
Add function to get ip for node based server (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
byn9826 authored Sep 5, 2024
1 parent d38ed63 commit 6f0978c
Show file tree
Hide file tree
Showing 22 changed files with 137 additions and 136 deletions.
4 changes: 2 additions & 2 deletions server/src/dtos/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import {
IsNotEmpty, IsOptional, IsString, IsUrl, Length,
} from 'class-validator'
import { ClientType } from 'shared'
import { formatUtil } from 'utils'
import { requestUtil } from 'utils'

const formatRedirectUri = (redirectUris: string[]) => redirectUris
.map((uri) => formatUtil.stripEndingSlash(uri.trim().toLowerCase()))
.map((uri) => requestUtil.stripEndingSlash(uri.trim().toLowerCase()))

export class PostAppReqDto {
@IsString()
Expand Down
4 changes: 2 additions & 2 deletions server/src/dtos/identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Context } from 'hono'
import { typeConfig } from 'configs'
import { oauthDto } from 'dtos'
import {
formatUtil, validateUtil,
requestUtil, validateUtil,
} from 'utils'
import { userModel } from 'models'

Expand Down Expand Up @@ -113,7 +113,7 @@ export const parseGetAuthorizeFollowUpReq = async (c: Context<typeConfig.Context
state: c.req.query('state') ?? '',
redirectUri: c.req.query('redirect_uri') ?? '',
code: c.req.query('code') ?? '',
locale: formatUtil.getLocaleFromQuery(
locale: requestUtil.getLocaleFromQuery(
c,
c.req.query('locale'),
),
Expand Down
22 changes: 11 additions & 11 deletions server/src/handlers/identity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
appService, consentService, emailService, jwtService, kvService, scopeService, sessionService, userService,
} from 'services'
import {
formatUtil, validateUtil,
requestUtil, validateUtil,
} from 'utils'
import {
AuthorizePasswordView, AuthorizeConsentView, AuthorizeAccountView,
Expand Down Expand Up @@ -159,7 +159,7 @@ export const getAuthorizePassword = async (c: Context<typeConfig.Context>) => {
GOOGLE_AUTH_CLIENT_ID: googleClientId,
} = env(c)

const queryString = formatUtil.getQueryString(c)
const queryString = requestUtil.getQueryString(c)

return c.html(<AuthorizePasswordView
queryString={queryString}
Expand All @@ -180,7 +180,7 @@ export const getAuthorizeReset = async (c: Context<typeConfig.Context>) => {
ENABLE_LOCALE_SELECTOR: enableLocaleSelector,
} = env(c)
const queryDto = await scopeService.parseGetAuthorizeDto(c)
const queryString = formatUtil.getQueryString(c)
const queryString = requestUtil.getQueryString(c)

return c.html(<AuthorizeResetView
queryString={queryString}
Expand All @@ -196,13 +196,13 @@ export const postResetCode = async (c: Context<typeConfig.Context>) => {
? String(reqBody.email).trim()
.toLowerCase()
: ''
const locale = formatUtil.getLocaleFromQuery(
const locale = requestUtil.getLocaleFromQuery(
c,
reqBody.locale,
)
if (!email) throw new errorConfig.Forbidden()

const ip = c.req.header('cf-connecting-ip') as string
const ip = requestUtil.getRequestIP(c)
const resetAttempts = await kvService.getPasswordResetAttemptsByIP(
c.env.KV,
email,
Expand Down Expand Up @@ -242,7 +242,7 @@ export const postAuthorizeReset = async (c: Context<typeConfig.Context>) => {
)

const { UNLOCK_ACCOUNT_VIA_PASSWORD_RESET: allowUnlock } = env(c)
const ip = c.req.header('cf-connecting-ip') as string
const ip = requestUtil.getRequestIP(c)
if (allowUnlock) {
await kvService.clearFailedLoginAttemptsByIP(
c.env.KV,
Expand All @@ -265,7 +265,7 @@ export const getAuthorizeAccount = async (c: Context<typeConfig.Context>) => {
ENABLE_LOCALE_SELECTOR: enableLocaleSelector,
} = env(c)

const queryString = formatUtil.getQueryString(c)
const queryString = requestUtil.getQueryString(c)

return c.html(<AuthorizeAccountView
locales={enableLocaleSelector ? locales : [queryDto.locale]}
Expand All @@ -288,7 +288,7 @@ export const postAuthorizeAccount = async (c: Context<typeConfig.Context>) => {
const parsedBody = {
...reqBody,
scopes: reqBody.scope.split(' '),
locale: formatUtil.getLocaleFromQuery(
locale: requestUtil.getLocaleFromQuery(
c,
reqBody.locale,
),
Expand Down Expand Up @@ -534,7 +534,7 @@ export const postAuthorizeOtpMfa = async (c: Context<typeConfig.Context>) => {

if (!authCodeStore.user.otpSecret) throw new errorConfig.Forbidden()

const ip = c.req.header('cf-connecting-ip') as string
const ip = requestUtil.getRequestIP(c)
const failedAttempts = await kvService.getFailedOtpMfaAttemptsByIP(
c.env.KV,
authCodeStore.user.id,
Expand Down Expand Up @@ -759,7 +759,7 @@ export const postAuthorizePassword = async (c: Context<typeConfig.Context>) => {
export const getVerifyEmail = async (c: Context<typeConfig.Context>) => {
const queryDto = new identityDto.GetVerifyEmailReqDto({
id: c.req.query('id') ?? '',
locale: formatUtil.getLocaleFromQuery(
locale: requestUtil.getLocaleFromQuery(
c,
c.req.query('locale'),
),
Expand Down Expand Up @@ -823,7 +823,7 @@ export const postLogout = async (c: Context<typeConfig.Context>) => {
}

const { AUTH_SERVER_URL } = env(c)
const redirectUri = `${formatUtil.stripEndingSlash(AUTH_SERVER_URL)}${routeConfig.InternalRoute.OAuth}/logout`
const redirectUri = `${requestUtil.stripEndingSlash(AUTH_SERVER_URL)}${routeConfig.InternalRoute.OAuth}/logout`

return c.json({
success: true,
Expand Down
4 changes: 2 additions & 2 deletions server/src/handlers/oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
appService, consentService, jwtService, kvService, roleService, scopeService, sessionService, userService,
} from 'services'
import {
cryptoUtil, formatUtil, timeUtil, validateUtil,
cryptoUtil, requestUtil, timeUtil, validateUtil,
} from 'utils'
import { userModel } from 'models'

Expand Down Expand Up @@ -62,7 +62,7 @@ export const getAuthorize = async (c: Context<typeConfig.Context>) => {
return c.redirect(url)
}

const queryString = formatUtil.getQueryString(c)
const queryString = requestUtil.getQueryString(c)
return c.redirect(`${routeConfig.InternalRoute.Identity}/authorize-password?${queryString}`)
}

Expand Down
4 changes: 2 additions & 2 deletions server/src/middlewares/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import {
errorConfig, localeConfig, typeConfig,
} from 'configs'
import { formatUtil } from 'utils'
import { requestUtil } from 'utils'
import { kvService } from 'services'

const store = new CookieStore()
Expand Down Expand Up @@ -46,7 +46,7 @@ export const validOrigin = async (
const origin = c.req.header('origin')
const { AUTH_SERVER_URL: serverUrl } = env(c)

if (formatUtil.stripEndingSlash(serverUrl) !== origin) {
if (requestUtil.stripEndingSlash(serverUrl) !== origin) {
throw new errorConfig.Forbidden(localeConfig.Error.WrongOrigin)
}

Expand Down
15 changes: 6 additions & 9 deletions server/src/models/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import { ClientType } from 'shared'
import {
adapterConfig, errorConfig,
} from 'configs'
import {
formatUtil,
validateUtil,
} from 'utils'
import { dbUtil } from 'utils'

export interface Common {
id: number;
Expand Down Expand Up @@ -93,7 +90,7 @@ export const create = async (
create.type,
create.redirectUris,
)
const result = await validateUtil.d1Run(stmt)
const result = await dbUtil.d1Run(stmt)
if (!result.success) throw new errorConfig.InternalServerError()
const id = result.meta.last_row_id
const record = await getById(
Expand All @@ -110,15 +107,15 @@ export const update = async (
const updateKeys: (keyof Update)[] = [
'name', 'redirectUris', 'isActive', 'deletedAt', 'updatedAt',
]
const stmt = formatUtil.d1UpdateQuery(
const stmt = dbUtil.d1UpdateQuery(
db,
TableName,
id,
updateKeys,
update,
)

const result = await validateUtil.d1Run(stmt)
const result = await dbUtil.d1Run(stmt)
if (!result.success) throw new errorConfig.InternalServerError()
const record = await getById(
db,
Expand All @@ -131,12 +128,12 @@ export const update = async (
export const remove = async (
db: D1Database, id: number,
): Promise<true> => {
const stmt = formatUtil.d1SoftDeleteQuery(
const stmt = dbUtil.d1SoftDeleteQuery(
db,
TableName,
id,
)

await validateUtil.d1Run(stmt)
await dbUtil.d1Run(stmt)
return true
}
14 changes: 6 additions & 8 deletions server/src/models/appScope.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { adapterConfig } from 'configs'
import {
formatUtil, validateUtil,
} from 'utils'
import { dbUtil } from 'utils'

export interface Record {
id: number;
Expand Down Expand Up @@ -52,7 +50,7 @@ export const create = async (
create.appId,
create.scopeId,
)
const result = await validateUtil.d1Run(stmt)
const result = await dbUtil.d1Run(stmt)
return result.success
}

Expand All @@ -62,28 +60,28 @@ export const update = async (
const updateKeys: (keyof Update)[] = [
'deletedAt', 'updatedAt',
]
const stmt = formatUtil.d1UpdateQuery(
const stmt = dbUtil.d1UpdateQuery(
db,
TableName,
id,
updateKeys,
update,
)

const result = await validateUtil.d1Run(stmt)
const result = await dbUtil.d1Run(stmt)
return result.success
}

export const remove = async (
db: D1Database, scopeId: number,
): Promise<true> => {
const stmt = formatUtil.d1SoftDeleteQuery(
const stmt = dbUtil.d1SoftDeleteQuery(
db,
TableName,
scopeId,
'scopeId',
)

await validateUtil.d1Run(stmt)
await dbUtil.d1Run(stmt)
return true
}
15 changes: 6 additions & 9 deletions server/src/models/role.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import {
adapterConfig, errorConfig,
} from 'configs'
import {
formatUtil,
validateUtil,
} from 'utils'
import { dbUtil } from 'utils'

export interface Record {
id: number;
Expand Down Expand Up @@ -56,7 +53,7 @@ export const create = async (
create.name,
create.note,
)
const result = await validateUtil.d1Run(stmt)
const result = await dbUtil.d1Run(stmt)
if (!result.success) throw new errorConfig.InternalServerError()
const id = result.meta.last_row_id

Expand All @@ -74,15 +71,15 @@ export const update = async (
const updateKeys: (keyof Update)[] = [
'name', 'updatedAt', 'note',
]
const stmt = formatUtil.d1UpdateQuery(
const stmt = dbUtil.d1UpdateQuery(
db,
TableName,
id,
updateKeys,
update,
)

const result = await validateUtil.d1Run(stmt)
const result = await dbUtil.d1Run(stmt)
if (!result.success) throw new errorConfig.InternalServerError()
const record = await getById(
db,
Expand All @@ -95,12 +92,12 @@ export const update = async (
export const remove = async (
db: D1Database, id: number,
): Promise<true> => {
const stmt = formatUtil.d1SoftDeleteQuery(
const stmt = dbUtil.d1SoftDeleteQuery(
db,
TableName,
id,
)

await validateUtil.d1Run(stmt)
await dbUtil.d1Run(stmt)
return true
}
15 changes: 6 additions & 9 deletions server/src/models/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import { ClientType } from 'shared'
import {
adapterConfig, errorConfig,
} from 'configs'
import {
formatUtil,
validateUtil,
} from 'utils'
import { dbUtil } from 'utils'
import { scopeLocaleModel } from 'models'

export interface Record {
Expand Down Expand Up @@ -77,7 +74,7 @@ export const create = async (
create.type,
create.note,
)
const result = await validateUtil.d1Run(stmt)
const result = await dbUtil.d1Run(stmt)
if (!result.success) throw new errorConfig.InternalServerError()
const id = result.meta.last_row_id
const record = await getById(
Expand All @@ -94,15 +91,15 @@ export const update = async (
const updateKeys: (keyof Update)[] = [
'name', 'deletedAt', 'updatedAt', 'note',
]
const stmt = formatUtil.d1UpdateQuery(
const stmt = dbUtil.d1UpdateQuery(
db,
TableName,
id,
updateKeys,
update,
)

const result = await validateUtil.d1Run(stmt)
const result = await dbUtil.d1Run(stmt)
if (!result.success) throw new errorConfig.InternalServerError()
const record = await getById(
db,
Expand All @@ -115,12 +112,12 @@ export const update = async (
export const remove = async (
db: D1Database, id: number,
): Promise<true> => {
const stmt = formatUtil.d1SoftDeleteQuery(
const stmt = dbUtil.d1SoftDeleteQuery(
db,
TableName,
id,
)

await validateUtil.d1Run(stmt)
await dbUtil.d1Run(stmt)
return true
}
Loading

0 comments on commit 6f0978c

Please sign in to comment.