refactor: apply auth and proxy middlewares

This commit is contained in:
Derrick Hammer 2024-02-14 00:41:02 -05:00
parent 829852c6c1
commit c084743b47
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 46 additions and 5 deletions

View File

@ -2,6 +2,8 @@ package account
import ( import (
"context" "context"
"crypto/ed25519"
"git.lumeweb.com/LumeWeb/portal/account" "git.lumeweb.com/LumeWeb/portal/account"
"git.lumeweb.com/LumeWeb/portal/api/middleware" "git.lumeweb.com/LumeWeb/portal/api/middleware"
"git.lumeweb.com/LumeWeb/portal/api/registry" "git.lumeweb.com/LumeWeb/portal/api/registry"
@ -18,6 +20,7 @@ type AccountAPI struct {
config *viper.Viper config *viper.Viper
accounts *account.AccountServiceDefault accounts *account.AccountServiceDefault
httpHandler *HttpHandler httpHandler *HttpHandler
identity ed25519.PrivateKey
} }
type AccountAPIParams struct { type AccountAPIParams struct {
@ -25,6 +28,7 @@ type AccountAPIParams struct {
Config *viper.Viper Config *viper.Viper
Accounts *account.AccountServiceDefault Accounts *account.AccountServiceDefault
HttpHandler *HttpHandler HttpHandler *HttpHandler
Identity ed25519.PrivateKey
} }
func NewS5(params AccountAPIParams) AccountApiResult { func NewS5(params AccountAPIParams) AccountApiResult {
@ -32,6 +36,7 @@ func NewS5(params AccountAPIParams) AccountApiResult {
config: params.Config, config: params.Config,
accounts: params.Accounts, accounts: params.Accounts,
httpHandler: params.HttpHandler, httpHandler: params.HttpHandler,
identity: params.Identity,
} }
return AccountApiResult{ return AccountApiResult{
@ -73,12 +78,27 @@ func (a AccountAPI) Stop(ctx context.Context) error {
} }
func getRoutes(a *AccountAPI) map[string]jape.Handler { func getRoutes(a *AccountAPI) map[string]jape.Handler {
authMw2fa := authMiddleware(middleware.AuthMiddlewareOptions{
Identity: a.identity,
Accounts: a.accounts,
Config: a.config,
Purpose: account.JWTPurpose2FA,
})
authMw := authMiddleware(middleware.AuthMiddlewareOptions{
Identity: a.identity,
Accounts: a.accounts,
Config: a.config,
Purpose: account.JWTPurposeLogin,
})
return map[string]jape.Handler{ return map[string]jape.Handler{
"/api/auth/login": a.httpHandler.login, "/api/auth/login": middleware.ApplyMiddlewares(a.httpHandler.login, authMw2fa, middleware.ProxyMiddleware),
"/api/auth/register": a.httpHandler.register, "/api/auth/register": a.httpHandler.register,
"/api/auth/otp/generate": a.httpHandler.otpGenerate, "/api/auth/otp/generate": middleware.ApplyMiddlewares(a.httpHandler.otpGenerate, authMw, middleware.ProxyMiddleware),
"/api/auth/otp/verify": a.httpHandler.otpVerify, "/api/auth/otp/verify": middleware.ApplyMiddlewares(a.httpHandler.otpVerify, authMw, middleware.ProxyMiddleware),
"/api/auth/otp/validate": a.httpHandler.otpValidate, "/api/auth/otp/validate": middleware.ApplyMiddlewares(a.httpHandler.otpValidate, authMw, middleware.ProxyMiddleware),
"/api/auth/otp/disable": a.httpHandler.otpDisable, "/api/auth/otp/disable": middleware.ApplyMiddlewares(a.httpHandler.otpDisable, authMw, middleware.ProxyMiddleware),
} }
} }

21
api/account/middleware.go Normal file
View File

@ -0,0 +1,21 @@
package account
import (
"net/http"
"git.lumeweb.com/LumeWeb/portal/api/middleware"
)
const (
authCookieName = "auth-token"
authQueryParam = "auth_token"
)
func findToken(r *http.Request) string {
return middleware.FindAuthToken(r, authCookieName, authQueryParam)
}
func authMiddleware(options middleware.AuthMiddlewareOptions) middleware.HttpMiddlewareFunc {
options.FindToken = findToken
return middleware.AuthMiddleware(options)
}