|
| 1 | +package auth |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/url" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/mxschmitt/golang-url-shortener/internal/util" |
| 10 | + "github.com/sirupsen/logrus" |
| 11 | + |
| 12 | + "github.com/pkg/errors" |
| 13 | + "golang.org/x/oauth2" |
| 14 | +) |
| 15 | + |
| 16 | +type oktaAdapter struct { |
| 17 | + config *oauth2.Config |
| 18 | +} |
| 19 | + |
| 20 | +// NewOktaAdapter creates an oAuth adapter out of the credentials and the baseURL |
| 21 | +func NewOktaAdapter(clientID, clientSecret, endpointURL string) Adapter { |
| 22 | + |
| 23 | + if endpointURL == "" { |
| 24 | + logrus.Error("Configure Okta Endpoint") |
| 25 | + } |
| 26 | + |
| 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 | + }} |
| 41 | +} |
| 42 | + |
| 43 | +func (a *oktaAdapter) GetRedirectURL(state string) string { |
| 44 | + return a.config.AuthCodeURL(state) |
| 45 | +} |
| 46 | + |
| 47 | +func (a *oktaAdapter) GetUserData(state, code string) (*user, error) { |
| 48 | + |
| 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 |
| 80 | +} |
| 81 | + |
| 82 | +func (a *oktaAdapter) GetOAuthProviderName() string { |
| 83 | + return "okta" |
| 84 | +} |
0 commit comments