digitaldreamer / twiliogo

Personal extension of xaviiic/twilioGo

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TwilioGo

A Golang library for twilio server side SDK.

NOTE: update adds video grant support

Installation

Install the package with go:

# original
go get github.com/xaviiic/twilioGo

# updated
go get github.com/digitaldreamer/twiliogo

Import the package to your go file:

import (
    "github.com/digitaldreamer/twiliogo"
)

Usage

Access Token

Access Tokens are short-lived tokens that you can use to authenticate Twilio Client SDKs like Video and IP Messaging. You create them on your server to verify a client's identity and grant access to client API features.

To create an access token, pass the Twilio API account information first.

// first create token with twilio api configurations
token := twiliogo.NewAccessToken(accountID, keyID, secret)
// setup token identity
token.SetIdentity(identity)

Once you have created a token instance, you can add access grant with desired API features.

// grant token access to programmable conversation API
grant := twiliogo.NewConversationGrant(configurationProfileID)
token.AddGrant(grant)

// grant token access to video API
grant := twiliogo.NewVideoGrant(room)
token.AddGrant(grant)

Then transform token to JWT format for client side usages.

jwt, err := token.ToJWT()

Capability Token

Capability tokens allow you to add Twilio capabilities to web and mobile applications without exposing your AuthToken in JavaScript or any other client-side environment.

Create a capability token instance first with API account information.

capability := twiliogo.NewCapability(accountID, authToken)

Then setup the capabilities of token and output a JWT format token.

// allow incoming connections
capability.AllowClientIncoming(clientName)
// allow outgoing connections
capability.AllowClientOutgoing(appID, nil)
jwt, err := capability.ToJWT()

Chaining

Generating token steps can be chained together.

token, err := twiliogo.NewAccessToken(accountSid, apiKey, apiSecret).
    SetIdentity(identity).
    AddGrant(twiliogo.NewConversationGrant(configurationSid)).
    SetTTL(30 * time.Minute).
    ToJWT()

License

Copyright 2016 xaviiic

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

Personal extension of xaviiic/twilioGo

License:Apache License 2.0


Languages

Language:Go 100.0%