auth

package
v0.0.0-...-0031c6d Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2018 License: GPL-3.0 Imports: 9 Imported by: 0

README

= Authorization (auth) package
Alessandro Sanino <a.sanino@arduino.cc>

This package allows to connect via OAuth2.0 to the arduino.cc system (Hydra)

See test package for an example of usage.

==== Basic usage
[source, go]
----
// Just create a new auth object.
authObj := auth.New()
token, err := auth.Token(testUser, testPass)
if err != nil {
	// Handle error
}

// Then use the generated token in your requests, for example:
// Obtain info of my user.
req, err := http.NewRequest("GET", "https://auth.arduino.cc/v1/users/byID/me", nil)
if err != nil {
	// Handle error
}
req.Header.Add("Authorization", "Bearer "+token.Access)
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
	// Handle error
}

// Before your token expires, you can do a refresh to have a new one for the 
// same session:
newToken, err := auth.Refresh(token.Access)
if err != nil {
	// Handle error
}
----

Documentation

Overview

Package auth uses the `oauth2 authorization_code` flow to authenticate with Arduino

If you have the username and password of a user, you can just instantiate a client with sane defaults:

client := auth.New()

and then call the Token method to obtain a Token object with an AccessToken and a RefreshToken

token, err := client.Token(username, password)

If instead you already have a token but want to refresh it, just call

token, err := client.refresh(refreshToken)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// CodeURL is the endpoint to redirect to obtain a code
	CodeURL string

	// TokenURL is the endpoint where you can request an access code
	TokenURL string

	// ClientID is the client id you are using
	ClientID string

	// RedirectURI is the redirectURI where the oauth process will redirect.
	// It's only required since the oauth system checks for it, but we intercept the redirect before hitting it
	RedirectURI string

	// Scopes is a space-separated list of scopes to require
	Scopes string
}

Config contains the variables you may want to change

func New

func New() *Config

New returns an auth configuration with sane defaults

func (*Config) Refresh

func (c *Config) Refresh(token string) (*Token, error)

Refresh exchanges a token for a new one

func (*Config) Token

func (c *Config) Token(user, pass string) (*Token, error)

Token authenticates with the given username and password and returns a Token object

type Token

type Token struct {
	// Access is the token to use to authenticate requests
	Access string `json:"access_token"`

	// Refresh is the token to use to request another access token. It's only returned if one of the scopes is "offline"
	Refresh string `json:"refresh_token"`

	// TTL is the number of seconds that the tokens will last
	TTL int `json:"expires_in"`

	// Scopes is a space-separated list of scopes associated to the access token
	Scopes string `json:"scope"`

	// Type is the type of token
	Type string `json:"token_type"`
}

Token is the response of the two authentication functions

Jump to

Keyboard shortcuts

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