auth

package
v0.0.0-...-3480b39 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2020 License: MIT Imports: 4 Imported by: 1

Documentation

Overview

Package auth exposes the Authenticator interface that allows for token generation.

There is usually no need to construct an Authenticator by itself since the interface is exposed via an Instance and this should be the primary entry point.

Example
package main

import (
	"net/http"

	"github.com/pusher/pusher-platform-go/auth"
	"github.com/pusher/pusher-platform-go/instance"
)

func main() {
	serviceInstance, err := instance.New(instance.Options{
		Locator:        "version:cluster:instance-id",
		Key:            "key:secret",
		ServiceName:    "service-name",
		ServiceVersion: "service-version",
	})
	if err != nil {
		// Do something with error
	}

	http.HandleFunc("/token", func(w http.ResponseWriter, req *http.Request) {
		// Get the user ID from url query params
		// Alternatively, this can come from the body
		userID := req.URL.Query().Get("user_id")
		if userID == "" {
			w.WriteHeader(http.StatusBadRequest)
			return
		}

		// Get an auth response
		authResponse, err := serviceInstance.Authenticate(auth.Payload{
			GrantType: auth.GrantTypeClientCredentials,
		}, auth.Options{
			UserID: &userID,
		})
		if err != nil {
			// Error when getting response
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		// Auth response contains an ErrorBody
		// Access it and respond back
		err = authResponse.Error()
		if err != nil {
			w.WriteHeader(authResponse.Status)
			w.Write([]byte(err.Error()))
			return
		}

		// Check if we have a token
		tokenResponse := authResponse.TokenResponse()
		if tokenResponse == nil {
			// Token was empty
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		// Things went well
		// Send back status and write the token
		w.WriteHeader(authResponse.Status)
		w.Write([]byte(tokenResponse.AccessToken))
		return
	})
}
Output:

Index

Examples

Constants

View Source
const GrantTypeClientCredentials = "client_credentials"

Variables

This section is empty.

Functions

This section is empty.

Types

type Authenticator

type Authenticator interface {
	Do(payload Payload, options Options) (*Response, error)
	GenerateAccessToken(options Options) (TokenWithExpiry, error)
}

Authenticator specifies the public facing interface for performing authentication and token generation.

func New

func New(instanceID, keyID, keySecret string) Authenticator

New returns a new instance of an authenticator that conforms to the Authenticator interface.

Example
package main

import (
	"github.com/pusher/pusher-platform-go/auth"
	"github.com/pusher/pusher-platform-go/instance"
)

func main() {
	locator := "v1:cluster:instance-id"
	key := "key:secret"

	locatorComponents, err := instance.ParseInstanceLocator(locator)
	if err != nil {
		// Do something with err
	}

	keyComponents, err := instance.ParseKey(key)
	if err != nil {
		// Do something with err
	}

	auth.New(locatorComponents.InstanceID, keyComponents.Key, keyComponents.Secret)
}
Output:

Example (Authenticate)
package main

import (
	"github.com/pusher/pusher-platform-go/auth"
	"github.com/pusher/pusher-platform-go/instance"
)

func main() {
	locator := "v1:cluster:instance-id"
	key := "key:secret"

	locatorComponents, err := instance.ParseInstanceLocator(locator)
	if err != nil {
		// Do something with err
	}

	keyComponents, err := instance.ParseKey(key)
	if err != nil {
		// Do something with err
	}

	userID := "test-user"
	authenticator := auth.New(locatorComponents.InstanceID, keyComponents.Key, keyComponents.Secret)
	_, err = authenticator.Do(auth.Payload{
		GrantType: auth.GrantTypeClientCredentials,
	}, auth.Options{
		UserID: &userID,
	})
	if err != nil {
		// Do something with error
	}
}
Output:

Example (GenerateAccessToken)
package main

import (
	"github.com/pusher/pusher-platform-go/auth"
	"github.com/pusher/pusher-platform-go/instance"
)

func main() {
	locator := "v1:cluster:instance-id"
	key := "key:secret"

	locatorComponents, err := instance.ParseInstanceLocator(locator)
	if err != nil {
		// Do something with err
	}

	keyComponents, err := instance.ParseKey(key)
	if err != nil {
		// Do something with err
	}

	userID := "test-user"
	authenticator := auth.New(locatorComponents.InstanceID, keyComponents.Key, keyComponents.Secret)
	_, err = authenticator.GenerateAccessToken(auth.Options{
		UserID: &userID,
	})
	if err != nil {
		// Do something with error
	}
}
Output:

type ErrorBody

type ErrorBody struct {
	ErrorType        string `json:"error"`
	ErrorDescription string `json:"error_description,omitempty"`
	ErrorURI         string `json:"error_uri,omitempty"`
}

ErrorBody is the corresponding structure of a platform error.

func (*ErrorBody) Error

func (e *ErrorBody) Error() string

Error conforms to the Error interface

type Options

type Options struct {
	UserID        *string                // Optional user id
	ServiceClaims map[string]interface{} // Optional JWT service claims
	Su            bool                   // Indicates if token should contain the `su` claim
	TokenExpiry   *time.Duration         // Optional token expiry (defaults to 24 hours)
}

Options contains information to configure Authenticate method calls.

type Payload

type Payload struct {
	GrantType string
}

Payload specifies the grant type for the token. Currently the only supported grant type is "client_credentials", passing anything else other than this will return an error

type Response

type Response struct {
	Status  int
	Headers http.Header
	Body    interface{}
}

Response represents data that is returned when making a call to the Authenticate method.

It returns the status of the response, headers and the response body which is either an ErrorBody or a TokenResponse.

func (*Response) Error

func (a *Response) Error() *ErrorBody

Error returns the ErrorBody of the authentication response.

Example
package main

import (
	"github.com/pusher/pusher-platform-go/auth"
	"github.com/pusher/pusher-platform-go/instance"
)

func main() {
	locator := "v1:cluster:instance-id"
	key := "key:secret"

	locatorComponents, err := instance.ParseInstanceLocator(locator)
	if err != nil {
		// Do something with err
	}

	keyComponents, err := instance.ParseKey(key)
	if err != nil {
		// Do something with err
	}

	userID := "test-user"
	authenticator := auth.New(locatorComponents.InstanceID, keyComponents.Key, keyComponents.Secret)
	authResponse, err := authenticator.Do(auth.Payload{
		GrantType: auth.GrantTypeClientCredentials,
	}, auth.Options{
		UserID: &userID,
	})
	if err != nil {
		// Do something with error
	}

	err = authResponse.Error()
	if err != nil {
		// Do something with error
	}
}
Output:

func (*Response) TokenResponse

func (a *Response) TokenResponse() *TokenResponse

TokenResponse returns the token returned by the response.

It is important to check if the Response has an associated ErrorBody by calling Error() before accessing the TokenResponse.

Example
package main

import (
	"github.com/pusher/pusher-platform-go/auth"
	"github.com/pusher/pusher-platform-go/instance"
)

func main() {
	locator := "v1:cluster:instance-id"
	key := "key:secret"

	locatorComponents, err := instance.ParseInstanceLocator(locator)
	if err != nil {
		// Do something with err
	}

	keyComponents, err := instance.ParseKey(key)
	if err != nil {
		// Do something with err
	}

	userID := "test-user"
	authenticator := auth.New(locatorComponents.InstanceID, keyComponents.Key, keyComponents.Secret)
	authResponse, err := authenticator.Do(auth.Payload{
		GrantType: auth.GrantTypeClientCredentials,
	}, auth.Options{
		UserID: &userID,
	})
	if err != nil {
		// Do something with error
	}

	err = authResponse.Error()
	if err != nil {
		// Do something with error
	}

	tokenResponse := authResponse.TokenResponse()
	if tokenResponse != nil {
		// Do something with response
	}
}
Output:

type TokenResponse

type TokenResponse struct {
	AccessToken string  `json:"access_token"`
	TokenType   string  `json:"token_type"`
	ExpiresIn   float64 `json:"expires_in"`
}

TokenResponse represents information that is returned on generation of a token.

type TokenWithExpiry

type TokenWithExpiry struct {
	Token     string  // Token string
	ExpiresIn float64 // Expiry in seconds
}

TokenWithExpiry represents a token that has an expiry time.

Jump to

Keyboard shortcuts

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