Skip to content

feature request: add support for authentication #11

@a-h

Description

@a-h

To add JWT authentication to an API, requires this... as per https://swagger.io/docs/specification/authentication/bearer-authentication/

	spec, err := api.Spec()
	if err != nil {
		log.Error("failed to create spec", slog.Any("error", err))
		os.Exit(1)
	}

	spec.Components.SecuritySchemes = map[string]*openapi3.SecuritySchemeRef{
		"bearerAuth": {
			Value: openapi3.NewJWTSecurityScheme(),
		},
	}

	securitySchemeToScopes := openapi3.NewSecurityRequirement()
	securitySchemeToScopes.Authenticate("bearerAuth")
	spec.Security = *openapi3.NewSecurityRequirements().
		With(securitySchemeToScopes)

Since it's so common, maybe it would be better as:

        api.SetAuth(rest.JWTBearerAuth())

	spec, err := api.Spec()
	if err != nil {
		log.Error("failed to create spec", slog.Any("error", err))
		os.Exit(1)
	}

Not sure about how the API would look to support mixed authentication (some handlers authenticated, some open). Maybe that would be out of scope.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions