|
1 | 1 | package auth |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "context" |
5 | | - "encoding/json" |
6 | | - "net/url" |
7 | | - "strings" |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "net/url" |
| 8 | + "strings" |
8 | 9 |
|
9 | | - "github.com/mxschmitt/golang-url-shortener/internal/util" |
10 | | - "github.com/sirupsen/logrus" |
| 10 | + "github.com/mxschmitt/golang-url-shortener/internal/util" |
| 11 | + "github.com/sirupsen/logrus" |
11 | 12 |
|
12 | | - "github.com/pkg/errors" |
13 | | - "golang.org/x/oauth2" |
| 13 | + "github.com/pkg/errors" |
| 14 | + "golang.org/x/oauth2" |
14 | 15 | ) |
15 | 16 |
|
16 | 17 | type oktaAdapter struct { |
17 | | - config *oauth2.Config |
| 18 | + config *oauth2.Config |
18 | 19 | } |
19 | 20 |
|
20 | 21 | // NewOktaAdapter creates an oAuth adapter out of the credentials and the baseURL |
21 | 22 | func NewOktaAdapter(clientID, clientSecret, endpointURL string) Adapter { |
22 | 23 |
|
23 | | - if endpointURL == "" { |
24 | | - logrus.Error("Configure Okta Endpoint") |
25 | | - } |
| 24 | + if endpointURL == "" { |
| 25 | + logrus.Error("Configure Okta Endpoint") |
| 26 | + } |
26 | 27 |
|
27 | | - return &oktaAdapter{&oauth2.Config{ |
28 | | - ClientID: clientID, |
29 | | - ClientSecret: clientSecret, |
30 | | - RedirectURL: util.GetConfig().BaseURL + "/api/v1/auth/okta/callback", |
31 | | - Scopes: []string{ |
32 | | - "profile", |
33 | | - "openid", |
34 | | - "offline_access", |
35 | | - }, |
36 | | - Endpoint: oauth2.Endpoint{ |
37 | | - AuthURL: endpointURL + "/v1/authorize", |
38 | | - TokenURL: endpointURL + "/v1/token", |
39 | | - }, |
40 | | - }} |
| 28 | + return &oktaAdapter{&oauth2.Config{ |
| 29 | + ClientID: clientID, |
| 30 | + ClientSecret: clientSecret, |
| 31 | + RedirectURL: util.GetConfig().BaseURL + "/api/v1/auth/okta/callback", |
| 32 | + Scopes: []string{ |
| 33 | + "profile", |
| 34 | + "openid", |
| 35 | + "offline_access", |
| 36 | + }, |
| 37 | + Endpoint: oauth2.Endpoint{ |
| 38 | + AuthURL: endpointURL + "/v1/authorize", |
| 39 | + TokenURL: endpointURL + "/v1/token", |
| 40 | + }, |
| 41 | + }} |
41 | 42 | } |
42 | 43 |
|
43 | 44 | func (a *oktaAdapter) GetRedirectURL(state string) string { |
44 | | - return a.config.AuthCodeURL(state) |
| 45 | + return a.config.AuthCodeURL(state) |
45 | 46 | } |
46 | 47 |
|
47 | 48 | func (a *oktaAdapter) GetUserData(state, code string) (*user, error) { |
48 | 49 |
|
49 | | - logrus.Debugf("Getting User Data with state: %s, and code: %s", state, code) |
50 | | - oAuthToken, err := a.config.Exchange(context.Background(), code) |
51 | | - if err != nil { |
52 | | - return nil, errors.Wrap(err, "could not exchange code") |
53 | | - } |
54 | | - if util.GetConfig().Okta.EndpointURL == "" { |
55 | | - logrus.Error("Okta EndpointURL is Empty") |
56 | | - } |
57 | | - oktaUrl, err := url.Parse(util.GetConfig().Okta.EndpointURL) |
58 | | - if err != nil { |
59 | | - return nil, errors.Wrap(err, "could not parse Okta EndpointURL") |
60 | | - } |
61 | | - oktaBaseURL := strings.Replace(oktaUrl.String(), oktaUrl.RequestURI(), "", 1) |
62 | | - oAuthUserInfoReq, err := a.config.Client(context.Background(), oAuthToken).Get(oktaBaseURL + "/api/v1/users/me") |
63 | | - if err != nil { |
64 | | - return nil, errors.Wrap(err, "could not get user data") |
65 | | - } |
66 | | - defer oAuthUserInfoReq.Body.Close() |
67 | | - var oUser struct { |
68 | | - ID int `json:"sub"` |
69 | | - // Custom URL property for user Avatar can go here |
70 | | - Name string `json:"name"` |
71 | | - } |
72 | | - if err = json.NewDecoder(oAuthUserInfoReq.Body).Decode(&oUser); err != nil { |
73 | | - return nil, errors.Wrap(err, "decoding user info failed") |
74 | | - } |
75 | | - return &user{ |
76 | | - ID: string(oUser.ID), |
77 | | - Name: oUser.Name, |
78 | | - Picture: util.GetConfig().BaseURL + "/images/okta_logo.png", // Default Okta Avatar |
79 | | - }, nil |
| 50 | + logrus.Debugf("Getting User Data with state: %s, and code: %s", state, code) |
| 51 | + oAuthToken, err := a.config.Exchange(context.Background(), code) |
| 52 | + if err != nil { |
| 53 | + return nil, errors.Wrap(err, "could not exchange code") |
| 54 | + } |
| 55 | + if util.GetConfig().Okta.EndpointURL == "" { |
| 56 | + logrus.Error("Okta EndpointURL is Empty") |
| 57 | + } |
| 58 | + oktaUrl, err := url.Parse(util.GetConfig().Okta.EndpointURL) |
| 59 | + if err != nil { |
| 60 | + return nil, errors.Wrap(err, "could not parse Okta EndpointURL") |
| 61 | + } |
| 62 | + oktaBaseURL := strings.Replace(oktaUrl.String(), oktaUrl.RequestURI(), "", 1) |
| 63 | + oAuthUserInfoReq, err := a.config.Client(context.Background(), oAuthToken).Get(oktaBaseURL + "/api/v1/users/me") |
| 64 | + if err != nil { |
| 65 | + return nil, errors.Wrap(err, "could not get user data") |
| 66 | + } |
| 67 | + defer oAuthUserInfoReq.Body.Close() |
| 68 | + var oUser struct { |
| 69 | + ID int `json:"sub"` |
| 70 | + // Custom URL property for user Avatar can go here |
| 71 | + Name string `json:"name"` |
| 72 | + } |
| 73 | + if err = json.NewDecoder(oAuthUserInfoReq.Body).Decode(&oUser); err != nil { |
| 74 | + return nil, errors.Wrap(err, "decoding user info failed") |
| 75 | + } |
| 76 | + return &user{ |
| 77 | + ID: fmt.Sprint(oUser.ID), |
| 78 | + Name: oUser.Name, |
| 79 | + Picture: util.GetConfig().BaseURL + "/images/okta_logo.png", // Default Okta Avatar |
| 80 | + }, nil |
80 | 81 | } |
81 | 82 |
|
82 | 83 | func (a *oktaAdapter) GetOAuthProviderName() string { |
83 | | - return "okta" |
| 84 | + return "okta" |
84 | 85 | } |
0 commit comments