client

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2025 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultClientTimeout is the default timeout for the client requests. It
	// can be overridden in the Config struct.
	DefaultClientTimeout = 30 * time.Second
	// DefaultAPIEndpoint is the default API endpoint for the client. It can be
	// overridden in the Config struct.
	DefaultAPIEndpoint       = "https://api.simpleauth.link"
	DefaultAuthTokenHeader   = "X-Auth-Token"
	DefaultAuthEmailHeader   = "X-Auth-User"
	DefaultAuthTokenURLParam = "token"
	DefaultAuthEmailURLParam = "user"
)

Variables

View Source
var (
	ErrInvalidAPIEndpoint        = fmt.Errorf("invalid API endpoint")
	ErrInvalidTimeout            = fmt.Errorf("invalid timeout value")
	ErrInvalidAppID              = fmt.Errorf("invalid application ID")
	ErrInvalidAppName            = fmt.Errorf("invalid application ID name")
	ErrInvalidAppRedirectURI     = fmt.Errorf("invalid application redirect URI")
	ErrInvalidAppSessionDuration = fmt.Errorf("invalid application session duration")
	ErrInvalidAppSecret          = fmt.Errorf("invalid application secret")
	ErrInvalidEmailAddress       = fmt.Errorf("invalid email address")
	ErrAPIUnavailable            = fmt.Errorf("API is unavailable")
	ErrRequestAppID              = fmt.Errorf("failed to request application ID")
	ErrRequestToken              = fmt.Errorf("failed to request token")
	ErrInvalidToken              = fmt.Errorf("invalid token provided")
	ErrCreateRequest             = fmt.Errorf("failed to create request")
	ErrMissingAuthHeaders        = fmt.Errorf("missing authentication headers")
	ErrMissingAuthURLParams      = fmt.Errorf("missing authentication URL parameters")
)

Functions

This section is empty.

Types

type Client

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

Client is the main struct for the API client. It holds the configuration and allows to make requests to the API. It is initialized with a Config struct that contains the API endpoint, application ID, application secret, and the timeout for requests. The client can be used to create new application IDs, request tokens, and verify tokens. It also checks if the API is reachable during initialization.

func Default

func Default(strAppID, appSecret string) (*Client, error)

Default method creates a new Client instance with the default configuration and the provided application ID and secret. Both inputs are required. The application ID is expected as a string to prevent the user from converting it.

func New

func New(cfg *Config) (*Client, error)

New method creates a new Client instance with the provided configuration. It initializes the configuration with default values if not set, checks if the API is reachable, and returns a new Client instance. If the API is not reachable, it returns an error.

func (*Client) AuthorizedRequestHeaders

func (c *Client) AuthorizedRequestHeaders(r *http.Request) (string, bool, error)

AuthorizedRequestHeaders method checks if the request is authorized by verifying the token and email from the headers of the request using the current client instance. It receives the request to check, and returns the user email in the request and a boolean that indicates if the token in the request is currently valid. If the client configuration is invalid, the request does not contains the required information or the validation fails, it returns an error.

func (*Client) AuthorizedRequestURLParams

func (c *Client) AuthorizedRequestURLParams(r *http.Request) (string, bool, error)

AuthorizedRequestURLParams method checks if the request is authorized by verifying the token and email from the URL params of the request using the current client instance. It receives the request to check, and returns the user email in the request and a boolean that indicates if the token in the request is currently valid. If the client configuration is invalid, the request does not contains the required information or the validation fails, it returns an error.

func (*Client) NewAppID

func (c *Client) NewAppID(name, redirectURI, secret string, sessionDuration time.Duration) (*token.AppID, error)

NewAppID method creates a new AppID with the provided name, redirect URI, secret, and session duration. It requires the client configuration to be valid, including the application ID and secret. The name, redirect URI, secret, and session duration must be valid. If the configuration is not valid, it returns an error. If the request is successful, it returns a new AppID instance with the generated ID. If the request fails, it returns an error.

func (*Client) RequestToken

func (c *Client) RequestToken(email string) error

RequestToken method requests a new token for the given email address. It requires the client configuration to be valid, including the application ID and secret. The email address must be a valid email format.

func (*Client) SetupAppID

func (c *Client) SetupAppID(appID *token.AppID, appSecret string)

SetupAppID sets the application ID and secret in the client configuration.

func (*Client) VerifyToken

func (c *Client) VerifyToken(token *token.Token, email string) (bool, time.Time, error)

VerifyToken method verifies the provided token for the given email address. It requires the client configuration to be valid, including the application ID and secret. The token must be valid, and the email address must be a valid email format. If the token is valid, it returns true and the expiration time. If the token is invalid or the email address is not valid, it returns false. If the request fails, it also returns an error.

type Config

type Config struct {
	APIEndpoint string
	AppID       *token.AppID
	AppSecret   string
	Timeout     time.Duration
}

Config holds the configuration for the client. It includes the API endpoint, application ID, application secret, and the timeout for requests. All fields are optional, but some of them are required for certain operations. If no API endpoint or timeout is provided, the default values will be used.

func (*Config) Validate

func (c *Config) Validate(requireApp bool) error

Validate method checks if the configuration is valid. It returns an error if the configuration is invalid. It receives a boolean parameter to indicate if it should validate or not the application ID and secret. If the parameter is true, it will check if the AppID is not nil and the AppSecret is not empty.

Jump to

Keyboard shortcuts

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