Skip to content

Commit d508a91

Browse files
committed
feat: go oauth
1 parent c434f0e commit d508a91

File tree

2 files changed

+20
-49
lines changed

2 files changed

+20
-49
lines changed

client/client.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
"bytes"
66
"encoding/json"
77
"fmt"
8-
"github.com/golang-jwt/jwt"
9-
"github.com/twilio/twilio-go"
108
"net/http"
119
"net/url"
1210
"regexp"
@@ -15,6 +13,7 @@ import (
1513
"strings"
1614
"time"
1715

16+
"github.com/golang-jwt/jwt"
1817
"github.com/pkg/errors"
1918
"github.com/twilio/twilio-go/client/form"
2019
)
@@ -44,8 +43,6 @@ type Client struct {
4443
accountSid string
4544
UserAgentExtensions []string
4645
BearerToken string
47-
*twilio.OrgTokenManager
48-
*twilio.ClientTokenManager
4946
}
5047

5148
// default http Client should not follow redirects and return the most recent response.
@@ -193,7 +190,6 @@ func (c *Client) SendRequest(method string, rawURL string, data url.Values,
193190
if !CheckTokenExpiry(c.BearerToken) {
194191
req.Header.Add("Authorization", "Bearer "+c.BearerToken)
195192
} else {
196-
197193
}
198194
} else if c.Username != "" {
199195
req.SetBasicAuth(c.basicAuth())

twilio.go

+19-44
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,6 @@ type Meta struct {
130130
URL *string `json:"url"`
131131
}
132132

133-
type ClientParams struct {
134-
Username string
135-
Password string
136-
AccountSid string
137-
Client client.BaseClient
138-
OrgClientCredentialProvider *OrgClientCredentialProvider
139-
ClientCredentialProvider *ClientCredentialProvider
140-
}
141-
142-
// CredentialProvider for Oauth Applications
143133
type CredentialProvider struct {
144134
AuthType string
145135
}
@@ -181,7 +171,7 @@ type ClientTokenManager struct {
181171
Scope string
182172
}
183173

184-
func GetOrgAccessToken(manager OrgTokenManager, apiService *PreviewIamTemp.ApiService) (*PreviewIamTemp.OauthV1Token, error) {
174+
func GetOrgAccessToken(manager OrgTokenManager) (*PreviewIamTemp.OauthV1Token, error) {
185175
params := &PreviewIamTemp.CreateTokenParams{}
186176
params.SetGrantType(manager.GrantType)
187177
params.SetClientId(manager.ClientId)
@@ -192,15 +182,15 @@ func GetOrgAccessToken(manager OrgTokenManager, apiService *PreviewIamTemp.ApiSe
192182
params.SetRefreshToken(manager.RefreshToken)
193183
params.SetScope(manager.Scope)
194184

195-
token, err := apiService.CreateToken(params)
185+
token, err := PreviewIamTemp.NewApiService(NewRestClient().RequestHandler).CreateToken(params)
196186
if err != nil {
197187
return nil, fmt.Errorf("failed to get access token: %w", err)
198188
}
199189

200190
return token, nil
201191
}
202192

203-
func GetClientAccessToken(manager ClientTokenManager, apiService *PreviewIamTemp.ApiService) (*PreviewIamTemp.OauthV1Token, error) {
193+
func GetClientAccessToken(manager ClientTokenManager) (*PreviewIamTemp.OauthV1Token, error) {
204194
params := &PreviewIamTemp.CreateTokenParams{}
205195
params.SetGrantType(manager.GrantType)
206196
params.SetClientId(manager.ClientId)
@@ -211,14 +201,23 @@ func GetClientAccessToken(manager ClientTokenManager, apiService *PreviewIamTemp
211201
params.SetRefreshToken(manager.RefreshToken)
212202
params.SetScope(manager.Scope)
213203

214-
token, err := apiService.CreateToken(params)
204+
token, err := PreviewIamTemp.NewApiService(NewRestClient().RequestHandler).CreateToken(params)
215205
if err != nil {
216206
return nil, fmt.Errorf("failed to get access token: %w", err)
217207
}
218208

219209
return token, nil
220210
}
221211

212+
type ClientParams struct {
213+
Username string
214+
Password string
215+
AccountSid string
216+
Client client.BaseClient
217+
OrgClientCredentialProvider *OrgClientCredentialProvider
218+
ClientCredentialProvider *ClientCredentialProvider
219+
}
220+
222221
// NewRestClientWithParams provides an initialized Twilio RestClient with params.
223222
func NewRestClientWithParams(params ClientParams) *RestClient {
224223
requestHandler := client.NewRequestHandler(params.Client)
@@ -257,19 +256,19 @@ func NewRestClientWithParams(params ClientParams) *RestClient {
257256
RefreshToken: "",
258257
Scope: ""}
259258

260-
token, err := GetOrgAccessToken(orgTokenManager, NewRestClient().PreviewIamToken)
259+
token, err := GetOrgAccessToken(orgTokenManager)
261260
//fmt.Println(*token.AccessToken)
262261
if err != nil {
263262
}
264-
fmt.Println(token)
263+
fmt.Println(token.AccessToken)
265264

266265
defaultClient := &client.Client{
267266
Credentials: client.NewCredentials("", ""),
268267
}
269268
if params.AccountSid != "" {
270269
defaultClient.SetAccountSid(params.AccountSid)
271270
}
272-
defaultClient.SetBearerToken("eyJhbGciOiJSUzI1NiIsImtpZCI6InVmQXhySkZ5VmNSNkdnQmZNQ29BTFoiLCJ0eXAiOiJKV1QifQ.eyJhdWQiOiJodHRwczovL2FwaS50d2lsaW8uY29tL3YyIiwiYXpwIjoiT1EwYmFkOWI1Yjc5ODEzNjhkOGI2NDFkNWFiY2MwOThjZCIsImlhdCI6MTczOTQ0OTIwMCwiZXhwIjoxNzM5NDUyODAwLCJpc3MiOiJodHRwczovL3ByZXZpZXctaWFtLnR3aWxpby5jb20iLCJzdWIiOiJPUTBiYWQ5YjViNzk4MTM2OGQ4YjY0MWQ1YWJjYzA5OGNkIiwiZ3R5IjoiY2xpZW50X2NyZWRlbnRpYWxzIiwiaHR0cDovL3R3aWxpby9hY3QiOnsic3ViIjoiQUM1NmM5YTA0ZDJlNjM5OTE1NWM3YzRhNTJmMDQ0YzEwNSJ9LCJodHRwOi8vdHdpbGlvL3R5cCI6InZuZC50d2lsaW8ub2F1dGguYXQrand0OyIsImh0dHA6Ly90d2lsaW8vc3ViIjoiT1EwYmFkOWI1Yjc5ODEzNjhkOGI2NDFkNWFiY2MwOThjZCIsImh0dHA6Ly90d2lsaW8vdmFsaWRyZWdpb25zIjoidXMxIn0.ColuyOS-CDSLmS0E16GU6GXHEt8Y5qxj4383S1r2j9YDI3swSKNfjpWvacevLJQBqxtBMZoawV7fyxZk-X0HmmlVJN_DUtwIY04u93BdM114chiKdL4rO49kxdzxdC6tViulWlFQzpvAg3hC1W-pgBIb8v48Z6SwpotZT3HM8VGVgEYPeFb3I6aQOpvNM9o1fNGFb4GWs6V78d9XvvjhvRsB58TfoFWEyfEwZZt-yWp__fmE-VaC5acnm3ATQ6TGInUiGzH6GqaDMtluj_TW0kefcawL7mJYjWFig9Mj27THk9ESa9H8JKm1-c0tJj0yVivp-UHN0ZfvcuDiHdhcEw")
271+
defaultClient.SetBearerToken("")
273272
requestHandler = client.NewRequestHandler(defaultClient)
274273
} else if params.ClientCredentialProvider != nil {
275274
// get AccessToken
@@ -284,19 +283,19 @@ func NewRestClientWithParams(params ClientParams) *RestClient {
284283
RefreshToken: "",
285284
Scope: ""}
286285

287-
token, err := GetClientAccessToken(clientTokenManager, NewRestClient().PreviewIamToken)
286+
token, err := GetClientAccessToken(clientTokenManager)
288287
//fmt.Println(*token.AccessToken)
289288
if err != nil {
290289
}
291-
fmt.Println(token)
290+
fmt.Println(token.AccessToken)
292291

293292
defaultClient := &client.Client{
294293
Credentials: client.NewCredentials("", ""),
295294
}
296295
if params.AccountSid != "" {
297296
defaultClient.SetAccountSid(params.AccountSid)
298297
}
299-
defaultClient.SetBearerToken("eyJhbGciOiJSUzI1NiIsImtpZCI6InVmQXhySkZ5VmNSNkdnQmZNQ29BTFoiLCJ0eXAiOiJKV1QifQ.eyJhdWQiOiJodHRwczovL2FwaS50d2lsaW8uY29tL3YyIiwiYXpwIjoiT1EwYmFkOWI1Yjc5ODEzNjhkOGI2NDFkNWFiY2MwOThjZCIsImlhdCI6MTczOTQ0OTIwMCwiZXhwIjoxNzM5NDUyODAwLCJpc3MiOiJodHRwczovL3ByZXZpZXctaWFtLnR3aWxpby5jb20iLCJzdWIiOiJPUTBiYWQ5YjViNzk4MTM2OGQ4YjY0MWQ1YWJjYzA5OGNkIiwiZ3R5IjoiY2xpZW50X2NyZWRlbnRpYWxzIiwiaHR0cDovL3R3aWxpby9hY3QiOnsic3ViIjoiQUM1NmM5YTA0ZDJlNjM5OTE1NWM3YzRhNTJmMDQ0YzEwNSJ9LCJodHRwOi8vdHdpbGlvL3R5cCI6InZuZC50d2lsaW8ub2F1dGguYXQrand0OyIsImh0dHA6Ly90d2lsaW8vc3ViIjoiT1EwYmFkOWI1Yjc5ODEzNjhkOGI2NDFkNWFiY2MwOThjZCIsImh0dHA6Ly90d2lsaW8vdmFsaWRyZWdpb25zIjoidXMxIn0.ColuyOS-CDSLmS0E16GU6GXHEt8Y5qxj4383S1r2j9YDI3swSKNfjpWvacevLJQBqxtBMZoawV7fyxZk-X0HmmlVJN_DUtwIY04u93BdM114chiKdL4rO49kxdzxdC6tViulWlFQzpvAg3hC1W-pgBIb8v48Z6SwpotZT3HM8VGVgEYPeFb3I6aQOpvNM9o1fNGFb4GWs6V78d9XvvjhvRsB58TfoFWEyfEwZZt-yWp__fmE-VaC5acnm3ATQ6TGInUiGzH6GqaDMtluj_TW0kefcawL7mJYjWFig9Mj27THk9ESa9H8JKm1-c0tJj0yVivp-UHN0ZfvcuDiHdhcEw")
298+
defaultClient.SetBearerToken("")
300299
requestHandler = client.NewRequestHandler(defaultClient)
301300
}
302301

@@ -355,30 +354,6 @@ func NewRestClientWithParams(params ClientParams) *RestClient {
355354
return c
356355
}
357356

358-
func NewRestClientWithClientCredentials(params ClientCredentialProvider) {
359-
// get AccessToken
360-
// set accessToken in client
361-
clientTokenManager := ClientTokenManager{
362-
GrantType: params.GrantType,
363-
ClientId: params.ClientId,
364-
ClientSecret: params.ClientSecret,
365-
Code: "",
366-
RedirectUri: "",
367-
Audience: "",
368-
RefreshToken: "",
369-
Scope: ""}
370-
371-
token, err := GetClientAccessToken(clientTokenManager, NewRestClient().PreviewIamToken)
372-
fmt.Println(*token.AccessToken)
373-
if err != nil {
374-
}
375-
376-
defaultClient := &client.Client{
377-
Credentials: client.NewCredentials("", ""),
378-
}
379-
defaultClient.SetBearerToken(*token.AccessToken)
380-
}
381-
382357
// NewRestClient provides an initialized Twilio RestClient.
383358
func NewRestClient() *RestClient {
384359
return NewRestClientWithParams(ClientParams{})

0 commit comments

Comments
 (0)