accesstoken

package module
v0.0.0-...-18ffa29 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2017 License: MIT Imports: 3 Imported by: 0

README

Go Library for Twilio Access Token Build Status

Go AccessToken creator for Twilio with support for grants. This library supports HS256, HS384, and HS512 hashing using a minimal JWT implementation.

Uses the static resources from: https://github.com/TwilioDevEd/video-quickstart-node

This is basically a port of the AccessToken portion of: https://github.com/twilio/twilio-node

Usage
  • Install the package
go get github.com/ZBoxApp/twilio-accesstoken-go
  • Grant access token to conversation:
	grant := accesstoken.NewConversationsGrant(os.Getenv("TW_VIDEO_SID"))
    	myToken.AddGrant(grant)
  • Grant access token to video API
	grant := accesstoken.NewVideoGrant(room) //Unique room name
	myToken.AddGrant((grant)
Sample

Sample app using credentials stored in your env

    package main

    import (
	    "encoding/json"
	    "fmt"
	    "github.com/ZBoxApp/twilio-accesstoken-go"
	    "net/http"
	    "os"
    )

    //Convenience interface for printing anonymous JSON objects
    type Response map[string]interface{}

    func (r Response) String() (s string) {
        b, err := json.Marshal(r)
        if err != nil {
            s = ""
        	return
        }

        s = string(b)
        return
    }

    func main() {
    	http.Handle("/", http.FileServer(http.Dir("./static")))
    	http.HandleFunc("/token", token)
    	http.ListenAndServe(":8080", nil)
    }

    // Handles POST request for token
    func token(w http.ResponseWriter, r *http.Request) {
    	if r.Method != "POST" {
    		return
    	}

    	// get credentials for environment variables
    	accountSid := os.Getenv("TW_ACCOUNT_SID")
    	apiKey := os.Getenv("TW_API_KEY")
    	apiSecret := os.Getenv("TW_API_SECRET")

    	// Create an Access Token
    	myToken := accesstoken.New(accountSid, apiKey, apiSecret)

    	// Set the Identity of this token
    	id := "gotwilio.sample"
    	myToken.Identity = id

    	// Grant access to Conversations
    	grant := accesstoken.NewConversationsGrant(os.Getenv("TW_VIDEO_SID"))
    	myToken.AddGrant(grant)

    	signedJWT, err := myToken.ToJWT(accesstoken.DefaultAlgorithm)

    	if err != nil {
    		w.WriteHeader(500)
    		w.Write([]byte(fmt.Sprintf("Error: %v", err)))
    		return
    	}

    	resp := &Response{
    		"identity": id,
    		"token":    signedJWT,
    	}

    	b, err := json.Marshal(resp)
    	if err != nil {
    		w.WriteHeader(500)
    		w.Write([]byte(fmt.Sprintf("Error: %v", err)))
    		return
    	}

    	// Return token info as JSON
    	w.WriteHeader(200)
    	w.Write(b)
    }

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultAlgorithm = jwt.HS256

DefaultAlgorithm is your preferred signing algorithm

Functions

This section is empty.

Types

type AccessToken

type AccessToken struct {
	Identity string // Generated here: https://www.twilio.com/user/account/video/profiles
	// contains filtered or unexported fields
}

AccessToken is a JWT that grants access to Twilio services

func New

func New(accountSid, apiKey, apiSecret string) *AccessToken

New creates a new AccessToken. TTL is set to default and grants are defaulted to an empty slice of Grant.

func (*AccessToken) AddGrant

func (t *AccessToken) AddGrant(grant Grant)

AddGrant adds a grant to this AccessToken

func (*AccessToken) ToJWT

func (t *AccessToken) ToJWT(algorithm string) (string, error)

ToJWT returns this token as a signed JWT using the specified hash algorithm Returns the signed JWT or an error Ported from: https://github.com/twilio/twilio-python/blob/master/twilio/access_token.py

type ConversationsGrant

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

ConversationsGrant is for Twilio Programmable Video access

func NewConversationsGrant

func NewConversationsGrant(sid string) *ConversationsGrant

NewConversationsGrant for Twilio Programmable Video access

type Grant

type Grant interface {
	// contains filtered or unexported methods
}

Grant is a Twilio SID resource that can be added to an AccessToken for extra services.

func NewVideoGrant

func NewVideoGrant(room string) Grant

type IPMessageGrant

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

IPMessageGrant is a grant for accessing Twilio IP Messaging

func NewIPMessageGrant

func NewIPMessageGrant(serviceSid, endpointID, deploymentRoleSid, pushCredentialSid string) *IPMessageGrant

NewIPMessageGrant for Twilio IP Message service

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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