edgegrid

package
v7.6.1 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2024 License: Apache-2.0 Imports: 18 Imported by: 9

README

edgegrid authorization library

This library provides Akamai .edgerc configuration parsing and http.Request signing.

EdgeGrid Configuration Files

The default location for the .edgerc file is $HOME/.edgerc. This file has a standard ini-file format. The default section is default. Multiple sections can be stored in the same ini-file for other configurations such as production, staging, or development.

[default]
client_secret = <default secret>
host = <default host>
access_token = <default access token>
client_token = <default client token>

[dev]
client_secret = <dev secret>
host = <dev host>
access_token = <dev access token>
client_token = <dev client token>

Basic Example

func main() {
    edgerc := Must(New())
    
    client := http.Client{}

    req, _ := http.NewRequest(http.MethodGet, "/papi/v1/contracts", nil)

    edgerc.SignRequest(req)

    resp, err := client.Do(req)
    if err != nil {
        log.Fataln(err)
    }

    // do something with response
}

Using a custom .edgerc file and section

    edgerc := Must(New(
        WithFile("/some/other/edgerc"),
        WithSection("production"),
    ))
}

Loading from environment variables

By default, it uses AKAMAI_HOST, AKAMAI_CLIENT_TOKEN, AKAMAI_CLIENT_SECRET, AKAMAI_ACCESS_TOKEN, and AKAMAI_MAX_BODY variables.

You can define multiple configurations by prefixing with the section name specified, e.g. passing "ccu" will cause it to look for AKAMAI_CCU_HOST, etc.

If AKAMAI_{SECTION} does not exist, it will fall back to just AKAMAI_.

    // Load from AKAMA_CCU_
    edgerc := Must(New(
        WithEnv(true),
        WithSection("ccu"),
    ))
}

Documentation

Overview

Package edgegrid provides Akamai .edgerc configuration parsing and http.Request signing.

Index

Constants

View Source
const (
	// DefaultConfigFile is the default configuration file path
	DefaultConfigFile = "~/.edgerc"

	// DefaultSection is the .edgerc ini default section
	DefaultSection = "default"

	// MaxBodySize is the max payload size for client requests
	MaxBodySize = 131072
)

Variables

View Source
var (
	// ErrRequiredOptionEnv is returned when a required ENV variable is not found
	ErrRequiredOptionEnv = errors.New("required option is missing from env")
	// ErrRequiredOptionEdgerc is returned when a required value is not found in edgerc file
	ErrRequiredOptionEdgerc = errors.New("required option is missing from edgerc")
	// ErrLoadingFile indicates problem with loading configuration file
	ErrLoadingFile = errors.New("loading config file")
	// ErrSectionDoesNotExist is returned when a section with provided name does not exist in edgerc
	ErrSectionDoesNotExist = errors.New("provided config section does not exist")
	// ErrHostContainsSlashAtTheEnd is returned when host has unnecessary '/' at the end
	ErrHostContainsSlashAtTheEnd = errors.New("host must not contain '/' at the end")
)

Functions

func Timestamp

func Timestamp(t time.Time) string

Timestamp returns an edgegrid timestamp from the time

Types

type Config

type Config struct {
	Host         string   `ini:"host"`
	ClientToken  string   `ini:"client_token"`
	ClientSecret string   `ini:"client_secret"`
	AccessToken  string   `ini:"access_token"`
	AccountKey   string   `ini:"account_key"`
	HeaderToSign []string `ini:"headers_to_sign"`
	MaxBody      int      `ini:"max_body"`
	RequestLimit int      `ini:"request_limit"`
	Debug        bool     `ini:"debug"`
	// contains filtered or unexported fields
}

Config struct provides all the necessary fields to create authorization header, debug is optional

func Must

func Must(config *Config, err error) *Config

Must will panic if the new method returns an error

func New

func New(opts ...Option) (*Config, error)

New returns new configuration with the specified options

func (Config) CheckRequestLimit

func (c Config) CheckRequestLimit(limit int)

CheckRequestLimit waits if necessary to ensure that OpenAPI's request limit is not exceeded

func (*Config) FromEnv

func (c *Config) FromEnv(section string) error

FromEnv creates a new config using the Environment (ENV)

By default, it uses AKAMAI_HOST, AKAMAI_CLIENT_TOKEN, AKAMAI_CLIENT_SECRET, AKAMAI_ACCESS_TOKEN and AKAMAI_MAX_BODY variables.

You can define multiple configurations by prefixing with the section name specified, e.g. passing "ccu" will cause it to look for AKAMAI_CCU_HOST, etc.

If AKAMAI_{SECTION} does not exist, it will fall back to just AKAMAI_.

func (*Config) FromFile

func (c *Config) FromFile(file string, section string) error

FromFile creates a config the configuration in standard INI format

func (Config) SignRequest

func (c Config) SignRequest(r *http.Request)

SignRequest adds a signed authorization header to the http request

func (*Config) Validate

func (c *Config) Validate() error

Validate verifies that the host is not ending with the slash character

type Option

type Option func(*Config)

Option defines a configuration option

func WithEnv

func WithEnv(env bool) Option

WithEnv sets the option to try to the environment vars to populate the config If loading from the env fails, will fallback to .edgerc

func WithFile

func WithFile(file string) Option

WithFile sets the config file path

func WithSection

func WithSection(section string) Option

WithSection sets the section in the config

type Signer

type Signer interface {
	SignRequest(r *http.Request)
	CheckRequestLimit(requestLimit int)
}

Signer is the request signer interface

Jump to

Keyboard shortcuts

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