This repository has been archived by the owner on Jul 19, 2023. It is now read-only.
forked from Vonage/vonage-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
122 lines (101 loc) · 2.4 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package nexmo
import (
"crypto/rsa"
"math/rand"
"strconv"
"time"
"fmt"
"github.com/dghubble/sling"
"github.com/dgrijalva/jwt-go"
)
type AuthType uint8
type RandomProvider interface {
Int31() int32
}
const (
ApiSecretAuth AuthType = iota + 1
ApiSecretPathAuth
JwtAuth
)
// API credentials to access the Nexmo APIs
type AuthSet struct {
apiSecret *apiSecretAuth
appAuth *applicationAuth
}
func NewAuthSet() *AuthSet {
return new(AuthSet)
}
func (a *AuthSet) ApplyAPICredentials(request apiSecretRequest) {
a.apiSecret.apply(request)
}
func (a *AuthSet) ApplyJWT(sling *sling.Sling) error {
token, err := a.GenerateToken()
if err != nil {
return err
}
sling.Set("Authorization", fmt.Sprintf("Bearer %s", token))
return nil
}
func (a *AuthSet) GenerateToken() (string, error) {
if a.appAuth == nil {
return "", fmt.Errorf("must call SetApplicationAuth before calling GenerateToken")
}
return a.appAuth.generateToken()
}
func (a *AuthSet) SetAPISecret(apiKey, apiSecret string) {
a.apiSecret = &apiSecretAuth{
apiKey: apiKey,
apiSecret: apiSecret,
}
}
func (a *AuthSet) SetApplicationAuth(appID string, key []byte) error {
privateKey, err := jwt.ParseRSAPrivateKeyFromPEM(key)
if err != nil {
return err
}
a.appAuth = &applicationAuth{
appID: appID,
privateKey: privateKey,
r: rand.New(rand.NewSource(time.Now().UnixNano())),
}
return nil
}
type apiSecretRequest interface {
setApiCredentials(apiKey, apiSecret string)
}
type apiSecretAuth struct {
apiKey string
apiSecret string
}
func (a apiSecretAuth) apply(request apiSecretRequest) error {
request.setApiCredentials(a.apiKey, a.apiSecret)
return nil
}
type jwtClaims struct {
ApplicationID string `json:"application_id"`
jwt.StandardClaims
}
type applicationAuth struct {
appID string
privateKey *rsa.PrivateKey
r RandomProvider
}
func (a applicationAuth) generateToken() (string, error) {
claims := jwtClaims{
a.appID,
jwt.StandardClaims{
Id: strconv.Itoa(int(a.r.Int31())),
IssuedAt: time.Now().UTC().Unix(),
},
}
token := jwt.NewWithClaims(jwt.GetSigningMethod("RS256"), claims)
return token.SignedString(a.privateKey)
}
type Credentials struct {
APIKey string `json:"api_key" url:"api_key"`
APISecret string `json:"api_secret" url:"api_secret"`
}
func (c *Credentials) setApiCredentials(apiKey, apiSecret string) {
c.APIKey = apiKey
c.APISecret = apiSecret
}