forked from Subito-it/vonage-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
68 lines (57 loc) · 1.87 KB
/
auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// This package offers a simple API wrapper and helper functions to get
// users started with the Vonage APIs
// Pull requests, issues and comments are all welcome and gratefully received
package vonage
import (
"github.com/adevinta/vonage-go-sdk/jwt"
)
// Auth types are various but support a common interface
type Auth interface {
GetCreds() []string
}
// KeySecretAuth is an Auth type to represent the API key and API secret combination
type KeySecretAuth struct {
apiKey string
apiSecret string
}
// GetCreds gives an array of credential strings
func (auth *KeySecretAuth) GetCreds() []string {
creds := []string{auth.apiKey, auth.apiSecret}
return creds
}
// CreateAuthFromKeySecret returns an Auth type given an API key and API secret
func CreateAuthFromKeySecret(apiKey string, apiSecret string) *KeySecretAuth {
auth := new(KeySecretAuth)
auth.apiKey = apiKey
auth.apiSecret = apiSecret
return auth
}
// JWTAuth is an Auth type to represent a JWT token
type JWTAuth struct {
JWT string
}
// GetCreds returns an array of strings, this time just one element which is
// the JWT token
func (auth *JWTAuth) GetCreds() []string {
creds := []string{auth.JWT}
return creds
}
// CreateAuthFromAppPrivateKey is a helper method to generate auth from an
// Application ID and a []byte of the private key (use with io.ReadFile)
func CreateAuthFromAppPrivateKey(appID string, privateKey []byte) (*JWTAuth, error) {
jwtGen := jwt.NewGenerator(appID, privateKey)
token, tokenErr := jwtGen.GenerateToken()
if tokenErr != nil {
return &JWTAuth{}, tokenErr
}
auth := new(JWTAuth)
auth.JWT = token
return auth, nil
}
// CreateAuthFromJwtTokenGenerator accepts a token generator struct, use this
// to set more of the options on the generator.
func CreateAuthFromJwtTokenGenerator(generator jwt.Generator) *JWTAuth {
auth := new(JWTAuth)
auth.JWT, _ = generator.GenerateToken()
return auth
}