authorize

package
v0.0.38 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BasicAuthHeader

func BasicAuthHeader(userid, password string) string

BasicAuthHeader creates a basic auth header.

func BasicAuthUserPass

func BasicAuthUserPass(userid, password string) string

BasicAuthUserPass base64 encodes userid and password and returns the encoded string.: https://datatracker.ietf.org/doc/html/rfc2617#section-2 user-pass = userid ":" password userid = *<TEXT excluding ":"> password = *TEXT

func ParseBasicAuthHeader

func ParseBasicAuthHeader(header string) (string, string, bool)

ParseBasicAuthHeader parses the basic authentication header and returns the username and password. It takes the authentication header string as input and returns the username, password, and a boolean indicating success. If the header is not in the correct format or decoding fails, it returns empty strings and false.

func SetRequestHeader

func SetRequestHeader(req *http.Request, auth *Authorize) error

SetRequestHeader sets the Authorization header of the provided http. Request using the encoded credentials from the given Authorize struct. It returns an error if there is an issue encoding the credentials.

Types

type Authorize

type Authorize struct {
	// Type is the type of authorization.
	Type Type
	// Credentials are the credentials associated with the authorization.
	Credentials string
}

Authorize represents an authorization.

func ParseHTTPRequest

func ParseHTTPRequest(req *http.Request) (*Authorize, bool)

ParseHTTPRequest parses the Authorization header from the provided HTTP request. If the header is empty, it returns nil and false.

func ParseHeader

func ParseHeader(auth string) (*Authorize, bool)

ParseHeader parses the authorization header and returns an Authorize struct with the type and credentials extracted. If the header is empty, it returns TypeUnknown with the provided auth string and false. If the header contains only one token, it returns TypeAnonymous with the token as credentials and true. For headers with multiple tokens, it checks the type (basic, bearer, digest) and returns the corresponding to Authorize struct with the credentials and true. If the type is not recognized, it returns TypeUnknown with the original auth string and false.

func ParseWWWRequest

func ParseWWWRequest(req *http.Request) (*Authorize, bool)

ParseWWWRequest parses the WWW-Authenticate header from the provided HTTP request. If the header is empty, it returns nil and false. TODO: Implement the parsing logic based on https://tools.ietf.org/html/rfc7235#section-4.1

func (Authorize) Encode

func (a Authorize) Encode() (string, error)

Encode encodes the Authorize struct into a string.

Returns the encoded string and an error if any.

type Type

type Type string

Type represents the type of authorization.

const (
	// TypeAnonymous represents an anonymous authorization.
	TypeAnonymous Type = "anonymous"
	// TypeBasic represents a basic authorization.
	TypeBasic Type = "basic"
	// TypeBearer represents a bearer authorization.
	TypeBearer Type = "bearer"
	// TypeDigest represents a digest authorization.
	TypeDigest Type = "digest"
	// TypeUnknown represents an unknown authorization.
	TypeUnknown Type = "unknown"
)

func (Type) Lower

func (t Type) Lower() string

Lower returns the lowercase string representation of the Type.

func (Type) String

func (t Type) String() string

String returns the string representation of the Type.

Jump to

Keyboard shortcuts

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