Skip to content

Commit

Permalink
wip: change path
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie authored Jul 24, 2024
1 parent e9b40b9 commit 4a6b328
Showing 1 changed file with 30 additions and 13 deletions.
43 changes: 30 additions & 13 deletions openfga/oas.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package openfga

import (
"context"
"fmt"

"github.com/getkin/kin-openapi/openapi3filter"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
"github.com/mitchellh/mapstructure"
middleware "github.com/oapi-codegen/fiber-middleware"
)

Expand Down Expand Up @@ -39,25 +41,40 @@ type OasBuilder interface {
}

// OasFGAAuthzBuilder ...
type OasFGAAuthzBuilder struct {
Options OasFGAAuthzBuilderOptions `json:"options"`
type OasFGAAuthzBuilder struct{}

// NewOasFGAAuthzBuilder ...
func NewOasFGAAuthzBuilder() *OasFGAAuthzBuilder {
return &OasFGAAuthzBuilder{}
}

// BuildWithContext ...
func (f *OasFGAAuthzBuilder) BuildWithContext(ctx context.Context, input *openapi3filter.AuthenticationInput) (User, Relation, Object, error) {
return f.User(ctx, input), f.Relation(ctx, input), f.Object(ctx, input), nil
opts := &OasFGAAuthzBuilderOptions{}

ext, ok := input.RequestValidationInput.Route.Operation.Extensions["x-fiber-authz-fga"]
if !ok {
return NoopUser, NoopRelation, NoopObject, fmt.Errorf("no x-fiber-authz-fga extension found")
}

err := mapstructure.Decode(ext, opts)

Check failure on line 60 in openfga/oas.go

View workflow job for this annotation

GitHub Actions / lint

the given struct should be annotated with the `mapstructure` tag (musttag)
if err != nil {
return NoopUser, NoopRelation, NoopObject, err
}

return f.User(ctx, input, opts), f.Relation(ctx, input, opts), f.Object(ctx, input, opts), nil
}

// User ...
func (f *OasFGAAuthzBuilder) User(ctx context.Context, input *openapi3filter.AuthenticationInput) User {
return NewUser(Namespace(f.Options.User.Namespace), OidcSubject(ctx))
func (f *OasFGAAuthzBuilder) User(ctx context.Context, input *openapi3filter.AuthenticationInput, opts *OasFGAAuthzBuilderOptions) User {
return NewUser(Namespace(opts.User.Namespace), OidcSubject(ctx))
}

// Object ...
func (f *OasFGAAuthzBuilder) Object(ctx context.Context, input *openapi3filter.AuthenticationInput) Object {
func (f *OasFGAAuthzBuilder) Object(ctx context.Context, input *openapi3filter.AuthenticationInput, opts *OasFGAAuthzBuilderOptions) Object {
ss := []string{}

for _, c := range f.Options.Object.Components {
for _, c := range opts.Object.Components {
switch c.In {
case "path":
ss = append(ss, PathParams(input.RequestValidationInput.PathParams, c.Name))
Expand All @@ -66,7 +83,12 @@ func (f *OasFGAAuthzBuilder) Object(ctx context.Context, input *openapi3filter.A
}
}

return NewObject(Namespace(f.Options.Object.Namespace), Join(f.Options.Object.Separator, ss...))
return NewObject(Namespace(opts.Object.Namespace), String(opts.Object.Name), Join(opts.Object.Separator, ss...))
}

// Relation ...
func (f *OasFGAAuthzBuilder) Relation(ctx context.Context, input *openapi3filter.AuthenticationInput, opts *OasFGAAuthzBuilderOptions) Relation {
return NewRelation(String(opts.Relation.Name))
}

// OasAuthenticateOpts ...
Expand Down Expand Up @@ -144,11 +166,6 @@ func PathParams(params map[string]string, name string, v ...string) string {
return p
}

// Relation ...
func (f *OasFGAAuthzBuilder) Relation(ctx context.Context, input *openapi3filter.AuthenticationInput) Relation {
return NewRelation(String(f.Options.User.Name))
}

// ResolverFunc is a function that resolves a user, relation, and object.
type ResolverFunc func(c *fiber.Ctx) (User, Relation, Object, error)

Expand Down

0 comments on commit 4a6b328

Please sign in to comment.