gin_jwks_rsa

package module
v0.0.0-...-cd6c39c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 17, 2022 License: MIT Imports: 8 Imported by: 1

README

gin-jwks-rsa Go Report Card CircleCI

This gin-gonic handler aims at providing a JKMS exposing the public key properties needed in the JWT encryption/decryption workflow using RSA.

Usage

Import your own private key
func main() {
    r := gin.Default()

    builder := NewConfigBuilder()
    config, err := builder.
        ImportPrivateKey().
        WithPath("../testdata/private.pem").
        WithKeyId("my-id").
        Build()

    if err != nil {
        fmt.Errorf("error generating conf %v", err)
        return
    }

    r.GET("/.well-known/jwks.json", Jkws(*config))
    r.Run()
}
Generate a private key
func main() {
    r := gin.Default()

    builder := NewConfigBuilder()
    config, err := builder.
        NewPrivateKey().
        WithKeyId("my-id").
        WithKeyLength(2048).
        Build()

    if err != nil {
        fmt.Errorf("error generating conf %v", err)
        return
    }

    r.GET("/.well-known/jwks.json", Jkws(*config))
    r.Run()
}
Output
{
    "keys": [
        {
            "kty": "RSA",
            "alg": "RS256",
            "e": "AQAB",
            "n": "6DGyBMjYcC5nf7eHHCqvwdgjr5_6_AnMbV124jtszu62vnMHHSIkVP6e5FWEQRUWXYww2cu-PKV2cJ1PcSvIs-OTwSayJnrQThsK5PzEAsH8pEhAoC2Izlpv4oK7vJYoUulcWTLFq0TcC0GkIZ3rUUn2RRAq508A0FI-ep17PjU7yamZAHwlfZPQ6NEFOnabBUE-qCaquv1PmNXV-PLZhhwAxkuxcGiZCaflkNmH8mw7L79zQWVAVgyIS68OV7CnblbuNwCOOzuLmnEJD3pwCfMq7a22vW_HXfVWzRqehkfgvH2Dmakbfm17WzFaWo_a8AUaU8ojY8DK-YxV0pU0ow",
            "use": "sig",
            "kid": "my-id"
        }
    ]
}

Documentation

Index

Constants

View Source
const KeyUsageAsSignature = "sig"

Variables

This section is empty.

Functions

func EncodeToString

func EncodeToString(src []byte) string

EncodeToString utility which converts []byte into a base64 string

func Jkws

func Jkws(config Config) gin.HandlerFunc

Jkws middleware exposing the public key properties required in order to decrypt a jwt token

Types

type Config

type Config struct {
	// contains filtered or unexported fields
}

Config represents the available options for the middleware.

type ConfigBuilder

type ConfigBuilder struct {
	// contains filtered or unexported fields
}

Config builder

func NewConfigBuilder

func NewConfigBuilder() *ConfigBuilder

Initialise a new config builder

func (*ConfigBuilder) Build

func (b *ConfigBuilder) Build() (*Config, error)

Build the config object in order to initiate the middleware

func (*ConfigBuilder) ImportPrivateKey

func (n *ConfigBuilder) ImportPrivateKey() *ConfigImportKeyBuilder

func (*ConfigBuilder) NewPrivateKey

func (n *ConfigBuilder) NewPrivateKey() *ConfigNewKeyBuilder

type ConfigImportKeyBuilder

type ConfigImportKeyBuilder struct {
	ConfigBuilder
}

Import key face of the config builder

func (*ConfigImportKeyBuilder) WithKeyId

Add a key id to the private key

func (*ConfigImportKeyBuilder) WithPath

func (n *ConfigImportKeyBuilder) WithPath(privateKeyPemPath string) *ConfigImportKeyBuilder

Add the private key path

type ConfigNewKeyBuilder

type ConfigNewKeyBuilder struct {
	ConfigBuilder
}

New key facet of the config builder

func (*ConfigNewKeyBuilder) WithKeyId

func (n *ConfigNewKeyBuilder) WithKeyId(keyId string) *ConfigNewKeyBuilder

Add a key id to the private key

func (*ConfigNewKeyBuilder) WithKeyLength

func (n *ConfigNewKeyBuilder) WithKeyLength(bits int) *ConfigNewKeyBuilder

Add the key length

type ImportKeyOptions

type ImportKeyOptions struct {
	// contains filtered or unexported fields
}

Structure used when the user imports an existing private key

func (*ImportKeyOptions) KeyId

func (o *ImportKeyOptions) KeyId() string

type JkwsResponse

type JkwsResponse struct {
	KeyTypeKey        string `json:"kty"`
	AlgorithmKey      string `json:"alg"`
	PubKeyExponentKey string `json:"e"`
	PubKeyModulusKey  string `json:"n"`
	KeyUsageKey       string `json:"use"`
	KeyIDKey          string `json:"kid"`
}

Refer to rfc for more information: https://www.rfc-editor.org/rfc/rfc7518#section-6.3.1

type NewKeyOptions

type NewKeyOptions struct {
	// contains filtered or unexported fields
}

Structure used when the user generates a new private key

func (*NewKeyOptions) KeyId

func (o *NewKeyOptions) KeyId() string

type Options

type Options interface {
	KeyId() string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL