Files
upstream-mirror c60cc7ca75
Some checks failed
CI / Run tests and lints (push) Failing after 29s
CI / Build CLI docs (push) Has been skipped
CI / Run Playwright end-to-end tests (mysql) (push) Failing after 3s
CI / Run Playwright end-to-end tests (postgres) (push) Failing after 2s
CI / Run Playwright end-to-end tests (sqlite) (push) Failing after 3s
CodeQL / Analyze (go) (push) Failing after 4s
CodeQL / Analyze (javascript) (push) Failing after 3s
Docker Image Scanners / scanners (push) Failing after 4s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Run end-to-end tests (postgres) (push) Failing after 19s
CI / Run end-to-end tests (sqlite) (push) Failing after 2s
CI / Run Playwright end-to-end tests (cockroach) (push) Failing after 2s
CI / Draft newsletter (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
snapshot of https://github.com/ory/kratos.git (master, 2026-09-15; blobs > 131072B omitted)
2026-09-15 21:19:12 +05:00

95 lines
2.1 KiB
Go

// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package request
import (
"fmt"
"net/http"
"github.com/hashicorp/go-retryablehttp"
)
type (
noopAuthStrategy struct{}
basicAuthStrategy struct {
user string
password string
}
apiKeyStrategy struct {
name string
value string
in string
}
AuthStrategy interface {
apply(req *retryablehttp.Request)
}
)
func authStrategy(typ string, config map[string]any) (AuthStrategy, error) {
switch typ {
case "":
return NewNoopAuthStrategy(), nil
case "api_key":
name, ok := config["name"].(string)
if !ok {
return nil, fmt.Errorf("api_key auth strategy requires a string name")
}
value, ok := config["value"].(string)
if !ok {
return nil, fmt.Errorf("api_key auth strategy requires a string value")
}
in, _ := config["in"].(string) // in is optional
return NewAPIKeyStrategy(in, name, value), nil
case "basic_auth":
user, ok := config["user"].(string)
if !ok {
return nil, fmt.Errorf("basic_auth auth strategy requires a string user")
}
password, ok := config["password"].(string)
if !ok {
return nil, fmt.Errorf("basic_auth auth strategy requires a string password")
}
return NewBasicAuthStrategy(user, password), nil
}
return nil, fmt.Errorf("unsupported auth type: %s", typ)
}
func NewNoopAuthStrategy() AuthStrategy {
return &noopAuthStrategy{}
}
func (c *noopAuthStrategy) apply(_ *retryablehttp.Request) {}
func NewBasicAuthStrategy(user, password string) AuthStrategy {
return &basicAuthStrategy{
user: user,
password: password,
}
}
func (c *basicAuthStrategy) apply(req *retryablehttp.Request) {
req.SetBasicAuth(c.user, c.password)
}
func NewAPIKeyStrategy(in, name, value string) AuthStrategy {
return &apiKeyStrategy{
in: in,
name: name,
value: value,
}
}
func (c *apiKeyStrategy) apply(req *retryablehttp.Request) {
switch c.in {
case "cookie":
req.AddCookie(&http.Cookie{Name: c.name, Value: c.value})
default:
// TODO add deprecation warning
fallthrough
case "header", "":
req.Header.Set(c.name, c.value)
}
}