-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
718 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//go:build !dev | ||
|
||
package middleware | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/auth0/go-jwt-middleware/v2/validator" | ||
"go.uber.org/zap" | ||
"net/http" | ||
) | ||
|
||
// getRawClaims extracts the raw claims from the request, however, it is meant | ||
// only to be used in dev mode so that unauthenticated requests can still succeed | ||
// such as requests to load the graphql schema. | ||
// | ||
// Any validation or parser errors are logged rather than returned. | ||
func getRawClaims( | ||
ctx context.Context, | ||
v *validator.Validator, | ||
_ *zap.Logger, | ||
r *http.Request, | ||
) (*validator.ValidatedClaims, error) { | ||
token, err := parseToken(r) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse token: %w", err) | ||
} | ||
raw, err := v.ValidateToken(ctx, token) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to validate token: %w", err) | ||
} | ||
if claims, ok := raw.(*validator.ValidatedClaims); ok { | ||
return claims, nil | ||
} | ||
return nil, fmt.Errorf("expected %T, got %T", &validator.ValidatedClaims{}, raw) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//go:build dev | ||
|
||
package middleware | ||
|
||
import ( | ||
"context" | ||
"github.com/auth0/go-jwt-middleware/v2/validator" | ||
"go.uber.org/zap" | ||
"net/http" | ||
) | ||
|
||
// getRawClaims extracts the raw claims from the request, however, it is meant | ||
// only to be used in dev mode so that unauthenticated requests can still succeed | ||
// such as requests to load the graphql schema. | ||
// | ||
// Any validation or parser errors are logged rather than returned. | ||
func getRawClaims( | ||
ctx context.Context, | ||
v *validator.Validator, | ||
logger *zap.Logger, | ||
r *http.Request, | ||
) (*validator.ValidatedClaims, error) { | ||
token, err := parseToken(r) | ||
if err != nil { | ||
logger.Error("auth failed in dev mode: failed to parse token", zap.Error(err)) | ||
return nil, nil | ||
} | ||
claims, err := v.ValidateToken(ctx, token) | ||
if err != nil { | ||
logger.Error("auth failed in dev mode: failed to validate token", zap.Error(err)) | ||
return nil, nil | ||
} | ||
return claims.(*validator.ValidatedClaims), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package appbackend | ||
|
||
import ( | ||
"fmt" | ||
"github.com/clarkmcc/cloudcore/app/backend/middleware" | ||
"github.com/graphql-go/graphql" | ||
) | ||
|
||
var projectCreate = &graphql.Field{ | ||
Type: graphql.NewObject(graphql.ObjectConfig{ | ||
Name: "ProjectCreate", | ||
Fields: graphql.Fields{ | ||
"project": &graphql.Field{ | ||
Type: projectType, | ||
}, | ||
"allProjects": &graphql.Field{ | ||
Type: graphql.NewList(projectType), | ||
}, | ||
}, | ||
}), | ||
Args: graphql.FieldConfigArgument{ | ||
"name": &graphql.ArgumentConfig{ | ||
Type: graphql.NewNonNull(graphql.String), | ||
}, | ||
"description": &graphql.ArgumentConfig{ | ||
Type: graphql.String, | ||
}, | ||
}, | ||
Resolve: wrapper(func(rctx resolveContext) (map[string]any, error) { | ||
sub := middleware.SubjectFromContext(rctx) | ||
project, err := rctx.db.CreateProject(rctx, sub, | ||
rctx.GetStringArg("name"), | ||
rctx.GetStringArg("description")) | ||
if err != nil { | ||
return nil, fmt.Errorf("creating new project: %w", err) | ||
} | ||
projects, err := rctx.db.GetUserProjects(rctx, sub) | ||
if err != nil { | ||
return nil, fmt.Errorf("getting user projects: %w", err) | ||
} | ||
return map[string]any{ | ||
"project": project, | ||
"allProjects": projects, | ||
}, nil | ||
}), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { | ||
Dialog, | ||
DialogContent, | ||
DialogDescription, | ||
DialogHeader, | ||
DialogTitle, | ||
} from "@/components/ui/dialog.tsx"; | ||
import { Label } from "@/components/ui/label.tsx"; | ||
import { Input } from "@/components/ui/input.tsx"; | ||
import { Button } from "@/components/ui/button.tsx"; | ||
import { useForm } from "react-hook-form"; | ||
import { useCreateProject } from "@/hooks/projects.ts"; | ||
|
||
type ProjectCreateDialogProps = { | ||
open: boolean; | ||
setOpen: (open: boolean) => void; | ||
}; | ||
|
||
interface Form { | ||
name: string; | ||
description?: string; | ||
} | ||
|
||
export function ProjectCreateDialog({ | ||
open, | ||
setOpen, | ||
}: ProjectCreateDialogProps) { | ||
const { register, handleSubmit } = useForm<Form>(); | ||
const [create, status] = useCreateProject(); | ||
|
||
async function onSubmit(data: Form) { | ||
await create(data.name, data.description); | ||
setOpen(false); | ||
} | ||
|
||
return ( | ||
<Dialog open={open} onOpenChange={setOpen}> | ||
<DialogContent> | ||
<DialogHeader> | ||
<DialogTitle>Create a new project</DialogTitle> | ||
<DialogDescription> | ||
Projects are used to organize your hosts and groups. They act is | ||
independent environments for you to manage your fleet. | ||
</DialogDescription> | ||
</DialogHeader> | ||
<form onSubmit={handleSubmit(onSubmit)}> | ||
<div className="space-y-4"> | ||
<div className="grid w-full items-center gap-1.5"> | ||
<Label htmlFor="name">Name</Label> | ||
<Input | ||
required | ||
type="name" | ||
id="name" | ||
placeholder="My project" | ||
{...register("name", { required: true })} | ||
/> | ||
</div> | ||
<div className="grid w-full items-center gap-1.5"> | ||
<Label htmlFor="description">Description</Label> | ||
<Input | ||
type="description" | ||
id="description" | ||
placeholder="..." | ||
{...register("description")} | ||
/> | ||
</div> | ||
<Button type="submit" disabled={status.loading} className="w-full"> | ||
Create | ||
</Button> | ||
</div> | ||
</form> | ||
</DialogContent> | ||
</Dialog> | ||
); | ||
} |
Oops, something went wrong.