mirror of
https://github.com/steveiliop56/tinyauth.git
synced 2026-06-26 05:10:15 +00:00
feat: add frontend
This commit is contained in:
@@ -6,6 +6,7 @@ type ScreenParams = {
|
|||||||
oidc_ticket?: string;
|
oidc_ticket?: string;
|
||||||
oidc_scope?: string;
|
oidc_scope?: string;
|
||||||
oidc_name?: string;
|
oidc_name?: string;
|
||||||
|
oidc_show_consent?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
const zodScreenParams = z.object({
|
const zodScreenParams = z.object({
|
||||||
@@ -14,6 +15,7 @@ const zodScreenParams = z.object({
|
|||||||
oidc_ticket: z.string().optional(),
|
oidc_ticket: z.string().optional(),
|
||||||
oidc_scope: z.string().optional(),
|
oidc_scope: z.string().optional(),
|
||||||
oidc_name: z.string().optional(),
|
oidc_name: z.string().optional(),
|
||||||
|
oidc_show_consent: z.stringbool().optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
export function useScreenParams(params: URLSearchParams): ScreenParams {
|
export function useScreenParams(params: URLSearchParams): ScreenParams {
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ import {
|
|||||||
recompileScreenParams,
|
recompileScreenParams,
|
||||||
useScreenParams,
|
useScreenParams,
|
||||||
} from "@/lib/hooks/screen-params";
|
} from "@/lib/hooks/screen-params";
|
||||||
|
import { useEffect } from "react";
|
||||||
|
|
||||||
type Scope = {
|
type Scope = {
|
||||||
id: string;
|
id: string;
|
||||||
@@ -90,25 +91,48 @@ export const AuthorizePage = () => {
|
|||||||
const isOidc = screenParams.login_for === "oidc";
|
const isOidc = screenParams.login_for === "oidc";
|
||||||
const compiledParams = recompileScreenParams(screenParams);
|
const compiledParams = recompileScreenParams(screenParams);
|
||||||
|
|
||||||
const authorizeMutation = useMutation({
|
const { mutate: authorizeMutate, isPending: authorizeIsPending } =
|
||||||
mutationFn: () => {
|
useMutation({
|
||||||
return axios.post("/api/oidc/authorize-complete", {
|
mutationFn: () => {
|
||||||
ticket: screenParams.oidc_ticket,
|
return axios.post("/api/oidc/authorize-complete", {
|
||||||
});
|
ticket: screenParams.oidc_ticket,
|
||||||
},
|
});
|
||||||
mutationKey: ["authorize", screenParams.oidc_ticket],
|
},
|
||||||
onSuccess: (data) => {
|
mutationKey: ["authorize", screenParams.oidc_ticket],
|
||||||
toast.info(t("authorizeSuccessTitle"), {
|
onSuccess: (data) => {
|
||||||
description: t("authorizeSuccessSubtitle"),
|
toast.info(t("authorizeSuccessTitle"), {
|
||||||
});
|
description: t("authorizeSuccessSubtitle"),
|
||||||
window.location.replace(data.data.redirect_uri);
|
});
|
||||||
},
|
window.location.replace(data.data.redirect_uri);
|
||||||
onError: (error) => {
|
},
|
||||||
window.location.replace(
|
onError: (error) => {
|
||||||
`/error?error=${encodeURIComponent(error.message)}`,
|
window.location.replace(
|
||||||
);
|
`/error?error=${encodeURIComponent(error.message)}`,
|
||||||
},
|
);
|
||||||
});
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (
|
||||||
|
!isOidc ||
|
||||||
|
screenParams.oidc_ticket === undefined ||
|
||||||
|
screenParams.oidc_scope === undefined ||
|
||||||
|
!auth.authenticated
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (screenParams.oidc_show_consent === false) {
|
||||||
|
authorizeMutate();
|
||||||
|
}
|
||||||
|
}, [
|
||||||
|
isOidc,
|
||||||
|
screenParams.oidc_ticket,
|
||||||
|
screenParams.oidc_scope,
|
||||||
|
screenParams.oidc_show_consent,
|
||||||
|
auth.authenticated,
|
||||||
|
authorizeMutate,
|
||||||
|
]);
|
||||||
|
|
||||||
if (
|
if (
|
||||||
!isOidc ||
|
!isOidc ||
|
||||||
@@ -130,6 +154,19 @@ export const AuthorizePage = () => {
|
|||||||
const scopes =
|
const scopes =
|
||||||
screenParams.oidc_scope.split(" ").filter((s) => s.trim() !== "") || [];
|
screenParams.oidc_scope.split(" ").filter((s) => s.trim() !== "") || [];
|
||||||
|
|
||||||
|
if (screenParams.oidc_show_consent === false) {
|
||||||
|
return (
|
||||||
|
<Card>
|
||||||
|
<CardHeader className="gap-1.5">
|
||||||
|
<CardTitle className="text-xl">Authorizing</CardTitle>
|
||||||
|
<CardDescription>
|
||||||
|
You will soon be redirected to your application...
|
||||||
|
</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
</Card>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Card>
|
<Card>
|
||||||
<CardHeader className="mb-2">
|
<CardHeader className="mb-2">
|
||||||
@@ -171,15 +208,12 @@ export const AuthorizePage = () => {
|
|||||||
</CardContent>
|
</CardContent>
|
||||||
)}
|
)}
|
||||||
<CardFooter className="flex flex-col items-stretch gap-3">
|
<CardFooter className="flex flex-col items-stretch gap-3">
|
||||||
<Button
|
<Button onClick={() => authorizeMutate()} loading={authorizeIsPending}>
|
||||||
onClick={() => authorizeMutation.mutate()}
|
|
||||||
loading={authorizeMutation.isPending}
|
|
||||||
>
|
|
||||||
{t("authorizeTitle")}
|
{t("authorizeTitle")}
|
||||||
</Button>
|
</Button>
|
||||||
<Button
|
<Button
|
||||||
onClick={() => navigate(`/logout${compiledParams}`)}
|
onClick={() => navigate(`/logout${compiledParams}`)}
|
||||||
disabled={authorizeMutation.isPending}
|
disabled={authorizeIsPending}
|
||||||
variant="outline"
|
variant="outline"
|
||||||
>
|
>
|
||||||
{t("cancelTitle")}
|
{t("cancelTitle")}
|
||||||
|
|||||||
@@ -58,8 +58,8 @@ func (app *BootstrapApp) setupRouter() error {
|
|||||||
apiRouter := engine.Group("/api")
|
apiRouter := engine.Group("/api")
|
||||||
|
|
||||||
controller.NewContextController(app.log, app.config, app.runtime, apiRouter)
|
controller.NewContextController(app.log, app.config, app.runtime, apiRouter)
|
||||||
controller.NewOAuthController(app.log, app.config, app.runtime, app.helpers, apiRouter, app.services.authService)
|
controller.NewOAuthController(app.log, app.config, app.runtime, &app.helpers, apiRouter, app.services.authService)
|
||||||
controller.NewOIDCController(app.log, app.services.oidcService, app.runtime, app.helpers, app.config, apiRouter, &engine.RouterGroup)
|
controller.NewOIDCController(app.log, app.services.oidcService, app.runtime, &app.helpers, app.config, apiRouter, &engine.RouterGroup)
|
||||||
controller.NewProxyController(app.log, app.runtime, apiRouter, app.services.accessControlService, app.services.authService, app.services.policyEngine)
|
controller.NewProxyController(app.log, app.runtime, apiRouter, app.services.accessControlService, app.services.authService, app.services.policyEngine)
|
||||||
controller.NewUserController(app.log, app.runtime, apiRouter, app.services.authService)
|
controller.NewUserController(app.log, app.runtime, apiRouter, app.services.authService)
|
||||||
controller.NewResourcesController(app.config, &engine.RouterGroup)
|
controller.NewResourcesController(app.config, &engine.RouterGroup)
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ func (app *BootstrapApp) setupServices() error {
|
|||||||
oauthBrokerService := service.NewOAuthBrokerService(app.log, app.runtime.OAuthProviders, app.ctx)
|
oauthBrokerService := service.NewOAuthBrokerService(app.log, app.runtime.OAuthProviders, app.ctx)
|
||||||
app.services.oauthBrokerService = oauthBrokerService
|
app.services.oauthBrokerService = oauthBrokerService
|
||||||
|
|
||||||
authService := service.NewAuthService(app.log, app.config, app.runtime, app.helpers, app.ctx, app.ding, app.services.ldapService, app.queries, app.services.oauthBrokerService, app.services.tailscaleService, app.services.policyEngine)
|
authService := service.NewAuthService(app.log, app.config, app.runtime, &app.helpers, app.ctx, app.ding, app.services.ldapService, app.queries, app.services.oauthBrokerService, app.services.tailscaleService, app.services.policyEngine)
|
||||||
app.services.authService = authService
|
app.services.authService = authService
|
||||||
|
|
||||||
oidcService, err := service.NewOIDCService(app.log, app.config, app.runtime, app.queries, app.ding)
|
oidcService, err := service.NewOIDCService(app.log, app.config, app.runtime, app.queries, app.ding)
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ type OAuthController struct {
|
|||||||
log *logger.Logger
|
log *logger.Logger
|
||||||
config model.Config
|
config model.Config
|
||||||
runtime model.RuntimeConfig
|
runtime model.RuntimeConfig
|
||||||
helpers model.RuntimeHelpers
|
helpers *model.RuntimeHelpers
|
||||||
auth *service.AuthService
|
auth *service.AuthService
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32,7 +32,7 @@ func NewOAuthController(
|
|||||||
log *logger.Logger,
|
log *logger.Logger,
|
||||||
config model.Config,
|
config model.Config,
|
||||||
runtimeConfig model.RuntimeConfig,
|
runtimeConfig model.RuntimeConfig,
|
||||||
helpers model.RuntimeHelpers,
|
helpers *model.RuntimeHelpers,
|
||||||
router *gin.RouterGroup,
|
router *gin.RouterGroup,
|
||||||
auth *service.AuthService,
|
auth *service.AuthService,
|
||||||
) *OAuthController {
|
) *OAuthController {
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ type OIDCController struct {
|
|||||||
log *logger.Logger
|
log *logger.Logger
|
||||||
oidc *service.OIDCService
|
oidc *service.OIDCService
|
||||||
runtime model.RuntimeConfig
|
runtime model.RuntimeConfig
|
||||||
helpers model.RuntimeHelpers
|
helpers *model.RuntimeHelpers
|
||||||
config model.Config
|
config model.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,7 +87,7 @@ func NewOIDCController(
|
|||||||
log *logger.Logger,
|
log *logger.Logger,
|
||||||
oidcService *service.OIDCService,
|
oidcService *service.OIDCService,
|
||||||
runtimeConfig model.RuntimeConfig,
|
runtimeConfig model.RuntimeConfig,
|
||||||
helpers model.RuntimeHelpers,
|
helpers *model.RuntimeHelpers,
|
||||||
config model.Config,
|
config model.Config,
|
||||||
router *gin.RouterGroup,
|
router *gin.RouterGroup,
|
||||||
mainRouter *gin.RouterGroup) *OIDCController {
|
mainRouter *gin.RouterGroup) *OIDCController {
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ type AuthService struct {
|
|||||||
log *logger.Logger
|
log *logger.Logger
|
||||||
config model.Config
|
config model.Config
|
||||||
runtime model.RuntimeConfig
|
runtime model.RuntimeConfig
|
||||||
helpers model.RuntimeHelpers
|
helpers *model.RuntimeHelpers
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
|
|
||||||
ldap *LdapService
|
ldap *LdapService
|
||||||
@@ -87,7 +87,7 @@ func NewAuthService(
|
|||||||
log *logger.Logger,
|
log *logger.Logger,
|
||||||
config model.Config,
|
config model.Config,
|
||||||
runtime model.RuntimeConfig,
|
runtime model.RuntimeConfig,
|
||||||
helpers model.RuntimeHelpers,
|
helpers *model.RuntimeHelpers,
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
dg *ding.Ding,
|
dg *ding.Ding,
|
||||||
ldap *LdapService,
|
ldap *LdapService,
|
||||||
|
|||||||
@@ -135,8 +135,8 @@ func CreateTestConfigs(t *testing.T) (model.Config, model.RuntimeConfig) {
|
|||||||
return config, runtime
|
return config, runtime
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateTestHelpers() model.RuntimeHelpers {
|
func CreateTestHelpers() *model.RuntimeHelpers {
|
||||||
return model.RuntimeHelpers{
|
return &model.RuntimeHelpers{
|
||||||
GetCookieDomain: func(ctx context.Context, ip string) (string, error) {
|
GetCookieDomain: func(ctx context.Context, ip string) (string, error) {
|
||||||
return "example.com", nil
|
return "example.com", nil
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user