Skip to content

Commit 24698de

Browse files
authored
Move oauth client code into client_oauth.go (#444)
Co-authored-by: Andrey Akhmedov <[email protected]>
1 parent 99adafa commit 24698de

File tree

2 files changed

+68
-69
lines changed

2 files changed

+68
-69
lines changed

pkg/client/client.go

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,22 @@
11
package client
22

33
import (
4-
"encoding/json"
54
"fmt"
65
"io"
76
"net/http"
87
"strings"
98

10-
"github.com/hashicorp/go-multierror"
119
"github.com/jetstack/preflight/api"
1210
)
1311

14-
var (
15-
// ClientID is the auth0 client identifier (injected at build time)
16-
ClientID string
17-
18-
// ClientSecret is the auth0 client secret (injected at build time)
19-
ClientSecret string
20-
21-
// AuthServerDomain is the auth0 domain (injected at build time)
22-
AuthServerDomain string
23-
)
24-
2512
type (
2613
// The Client interface describes types that perform requests against the Jetstack Secure backend.
2714
Client interface {
2815
PostDataReadings(orgID, clusterID string, readings []*api.DataReading) error
2916
Post(path string, body io.Reader) (*http.Response, error)
3017
}
31-
32-
// Credentials defines the format of the credentials.json file.
33-
Credentials struct {
34-
// UserID is the ID or email for the user or service account.
35-
UserID string `json:"user_id"`
36-
// UserSecret is the secret for the user or service account.
37-
UserSecret string `json:"user_secret"`
38-
// The following fields are optional as the default behaviour
39-
// is to use the equivalent variables defined at package level
40-
// and injected at build time.
41-
// ClientID is the oauth2 client ID.
42-
ClientID string `json:"client_id,omitempty"`
43-
// ClientSecret is the oauth2 client secret.
44-
ClientSecret string `json:"client_secret,omitempty"`
45-
// AuthServerDomain is the domain for the auth server.
46-
AuthServerDomain string `json:"auth_server_domain,omitempty"`
47-
}
4818
)
4919

50-
// ParseCredentials reads credentials into a struct used. Performs validations.
51-
func ParseCredentials(data []byte) (*Credentials, error) {
52-
var credentials Credentials
53-
54-
err := json.Unmarshal(data, &credentials)
55-
if err != nil {
56-
return nil, err
57-
}
58-
59-
if err = credentials.validate(); err != nil {
60-
return nil, err
61-
}
62-
63-
return &credentials, nil
64-
}
65-
66-
// IsClientSet returns whether the client credentials are set or not.
67-
func (c *Credentials) IsClientSet() bool {
68-
return c.ClientID != "" && c.ClientSecret != "" && c.AuthServerDomain != ""
69-
}
70-
71-
func (c *Credentials) validate() error {
72-
var result *multierror.Error
73-
74-
if c == nil {
75-
return fmt.Errorf("credentials are nil")
76-
}
77-
78-
if c.UserID == "" {
79-
result = multierror.Append(result, fmt.Errorf("user_id cannot be empty"))
80-
}
81-
82-
if c.UserSecret == "" {
83-
result = multierror.Append(result, fmt.Errorf("user_secret cannot be empty"))
84-
}
85-
86-
return result.ErrorOrNil()
87-
}
88-
8920
func fullURL(baseURL, path string) string {
9021
base := baseURL
9122
for strings.HasSuffix(base, "/") {

pkg/client/client_oauth.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"strings"
1313
"time"
1414

15+
"github.com/hashicorp/go-multierror"
1516
"github.com/jetstack/preflight/api"
1617
"github.com/juju/errors"
1718
)
@@ -31,6 +32,34 @@ type (
3132
bearer string
3233
expirationDate time.Time
3334
}
35+
36+
// Credentials defines the format of the credentials.json file.
37+
Credentials struct {
38+
// UserID is the ID or email for the user or service account.
39+
UserID string `json:"user_id"`
40+
// UserSecret is the secret for the user or service account.
41+
UserSecret string `json:"user_secret"`
42+
// The following fields are optional as the default behaviour
43+
// is to use the equivalent variables defined at package level
44+
// and injected at build time.
45+
// ClientID is the oauth2 client ID.
46+
ClientID string `json:"client_id,omitempty"`
47+
// ClientSecret is the oauth2 client secret.
48+
ClientSecret string `json:"client_secret,omitempty"`
49+
// AuthServerDomain is the domain for the auth server.
50+
AuthServerDomain string `json:"auth_server_domain,omitempty"`
51+
}
52+
)
53+
54+
var (
55+
// ClientID is the auth0 client identifier (injected at build time)
56+
ClientID string
57+
58+
// ClientSecret is the auth0 client secret (injected at build time)
59+
ClientSecret string
60+
61+
// AuthServerDomain is the auth0 domain (injected at build time)
62+
AuthServerDomain string
3463
)
3564

3665
func (t *accessToken) needsRenew() bool {
@@ -184,3 +213,42 @@ func (c *OAuthClient) renewAccessToken() error {
184213

185214
return nil
186215
}
216+
217+
// ParseCredentials reads credentials into a struct used. Performs validations.
218+
func ParseCredentials(data []byte) (*Credentials, error) {
219+
var credentials Credentials
220+
221+
err := json.Unmarshal(data, &credentials)
222+
if err != nil {
223+
return nil, err
224+
}
225+
226+
if err = credentials.validate(); err != nil {
227+
return nil, err
228+
}
229+
230+
return &credentials, nil
231+
}
232+
233+
// IsClientSet returns whether the client credentials are set or not.
234+
func (c *Credentials) IsClientSet() bool {
235+
return c.ClientID != "" && c.ClientSecret != "" && c.AuthServerDomain != ""
236+
}
237+
238+
func (c *Credentials) validate() error {
239+
var result *multierror.Error
240+
241+
if c == nil {
242+
return fmt.Errorf("credentials are nil")
243+
}
244+
245+
if c.UserID == "" {
246+
result = multierror.Append(result, fmt.Errorf("user_id cannot be empty"))
247+
}
248+
249+
if c.UserSecret == "" {
250+
result = multierror.Append(result, fmt.Errorf("user_secret cannot be empty"))
251+
}
252+
253+
return result.ErrorOrNil()
254+
}

0 commit comments

Comments
 (0)