diff --git a/packages/types/src/api/web/auth.ts b/packages/types/src/api/web/auth.ts index d631762b30..0964d71576 100644 --- a/packages/types/src/api/web/auth.ts +++ b/packages/types/src/api/web/auth.ts @@ -3,14 +3,28 @@ export interface LoginRequest { password: string } +export interface LogoutResponse { + message: string +} + +export interface SetInitInfoRequest extends Record {} + +export interface GetInitInfoResponse extends Record {} + export interface PasswordResetRequest { email: string } +export interface PasswordResetResponse { + message: string +} export interface PasswordResetUpdateRequest { resetCode: string password: string } +export interface PasswordResetUpdateResponse { + message: string +} export interface UpdateSelfRequest { firstName?: string diff --git a/packages/worker/src/api/controllers/global/auth.ts b/packages/worker/src/api/controllers/global/auth.ts index 530a1df102..1e6b0d3aab 100644 --- a/packages/worker/src/api/controllers/global/auth.ts +++ b/packages/worker/src/api/controllers/global/auth.ts @@ -16,8 +16,15 @@ import { PasswordResetUpdateRequest, GoogleInnerConfig, DatasourceAuthCookie, + LogoutResponse, + UserCtx, + SetInitInfoRequest, + GetInitInfoResponse, + PasswordResetResponse, + PasswordResetUpdateResponse, } from "@budibase/types" import env from "../../../environment" +import { Next } from "koa" import * as authSdk from "../../../sdk/auth" import * as userSdk from "../../../sdk/users" @@ -52,7 +59,7 @@ async function passportCallback( ctx.set(Header.TOKEN, token) } -export const login = async (ctx: Ctx, next: any) => { +export const login = async (ctx: Ctx, next: Next) => { const email = ctx.request.body.username const user = await userSdk.db.getUserByEmail(email) @@ -72,7 +79,7 @@ export const login = async (ctx: Ctx, next: any) => { )(ctx, next) } -export const logout = async (ctx: any) => { +export const logout = async (ctx: UserCtx) => { if (ctx.user && ctx.user._id) { await authSdk.logout({ ctx, userId: ctx.user._id }) } @@ -81,13 +88,13 @@ export const logout = async (ctx: any) => { // INIT -export const setInitInfo = (ctx: any) => { +export const setInitInfo = (ctx: UserCtx) => { const initInfo = ctx.request.body setCookie(ctx, initInfo, Cookie.Init) ctx.status = 200 } -export const getInitInfo = (ctx: any) => { +export const getInitInfo = (ctx: UserCtx) => { try { ctx.body = getCookie(ctx, Cookie.Init) || {} } catch (err) { @@ -101,7 +108,9 @@ export const getInitInfo = (ctx: any) => { /** * Reset the user password, used as part of a forgotten password flow. */ -export const reset = async (ctx: Ctx) => { +export const reset = async ( + ctx: Ctx +) => { const { email } = ctx.request.body await authSdk.reset(email) @@ -114,7 +123,9 @@ export const reset = async (ctx: Ctx) => { /** * Perform the user password update if the provided reset code is valid. */ -export const resetUpdate = async (ctx: Ctx) => { +export const resetUpdate = async ( + ctx: Ctx +) => { const { resetCode, password } = ctx.request.body try { await authSdk.resetUpdate(resetCode, password) @@ -130,7 +141,10 @@ export const resetUpdate = async (ctx: Ctx) => { // DATASOURCE -export const datasourcePreAuth = async (ctx: any, next: any) => { +export const datasourcePreAuth = async ( + ctx: UserCtx, + next: Next +) => { const provider = ctx.params.provider const { middleware } = require(`@budibase/backend-core`) const handler = middleware.datasource[provider] @@ -147,7 +161,7 @@ export const datasourcePreAuth = async (ctx: any, next: any) => { return handler.preAuth(passport, ctx, next) } -export const datasourceAuth = async (ctx: any, next: any) => { +export const datasourceAuth = async (ctx: UserCtx, next: Next) => { const authStateCookie = getCookie( ctx, Cookie.DatasourceAuth @@ -171,7 +185,7 @@ export async function googleCallbackUrl(config?: GoogleInnerConfig) { * The initial call that google authentication makes to take you to the google login screen. * On a successful login, you will be redirected to the googleAuth callback route. */ -export const googlePreAuth = async (ctx: any, next: any) => { +export const googlePreAuth = async (ctx: Ctx, next: Next) => { const config = await configs.getGoogleConfig() if (!config) { return ctx.throw(400, "Google config not found") @@ -190,7 +204,7 @@ export const googlePreAuth = async (ctx: any, next: any) => { })(ctx, next) } -export const googleCallback = async (ctx: any, next: any) => { +export const googleCallback = async (ctx: Ctx, next: Next) => { const config = await configs.getGoogleConfig() if (!config) { return ctx.throw(400, "Google config not found") @@ -241,7 +255,7 @@ export const oidcStrategyFactory = async (ctx: any) => { * The initial call that OIDC authentication makes to take you to the configured OIDC login screen. * On a successful login, you will be redirected to the oidcAuth callback route. */ -export const oidcPreAuth = async (ctx: Ctx, next: any) => { +export const oidcPreAuth = async (ctx: Ctx, next: Next) => { const { configId } = ctx.params if (!configId) { ctx.throw(400, "OIDC config id is required") @@ -266,7 +280,7 @@ export const oidcPreAuth = async (ctx: Ctx, next: any) => { })(ctx, next) } -export const oidcCallback = async (ctx: any, next: any) => { +export const oidcCallback = async (ctx: Ctx, next: Next) => { const strategy = await oidcStrategyFactory(ctx) return passport.authenticate(