diff --git a/examples/main.go b/examples/main.go index 6f421d81a..a40643b35 100644 --- a/examples/main.go +++ b/examples/main.go @@ -15,6 +15,7 @@ import ( "github.com/markbates/goth/providers/apple" "github.com/markbates/goth/providers/auth0" "github.com/markbates/goth/providers/azuread" + "github.com/markbates/goth/providers/azuredevops" "github.com/markbates/goth/providers/battlenet" "github.com/markbates/goth/providers/bitbucket" "github.com/markbates/goth/providers/box" @@ -108,6 +109,7 @@ func main() { yammer.New(os.Getenv("YAMMER_KEY"), os.Getenv("YAMMER_SECRET"), "http://localhost:3000/auth/yammer/callback"), onedrive.New(os.Getenv("ONEDRIVE_KEY"), os.Getenv("ONEDRIVE_SECRET"), "http://localhost:3000/auth/onedrive/callback"), azuread.New(os.Getenv("AZUREAD_KEY"), os.Getenv("AZUREAD_SECRET"), "http://localhost:3000/auth/azuread/callback", nil), + azuredevops.New(os.Getenv("AZUREDEVOPS_KEY"), os.Getenv("AZUREDEVOPS_SECRET"), "http://localhost:3000/auth/azuredevops/callback"), microsoftonline.New(os.Getenv("MICROSOFTONLINE_KEY"), os.Getenv("MICROSOFTONLINE_SECRET"), "http://localhost:3000/auth/microsoftonline/callback"), battlenet.New(os.Getenv("BATTLENET_KEY"), os.Getenv("BATTLENET_SECRET"), "http://localhost:3000/auth/battlenet/callback"), eveonline.New(os.Getenv("EVEONLINE_KEY"), os.Getenv("EVEONLINE_SECRET"), "http://localhost:3000/auth/eveonline/callback"), diff --git a/providers/azuredevops/azuredevops.go b/providers/azuredevops/azuredevops.go new file mode 100644 index 000000000..7dacaeb0e --- /dev/null +++ b/providers/azuredevops/azuredevops.go @@ -0,0 +1,241 @@ +// Package azure implements the OAuth2 protocol for authenticating users through Azure. +// This package can be used as a reference implementation of an OAuth2 provider for Goth. +package azuredevops + +import ( + "bytes" + "encoding/json" + "fmt" + "io" + "io/ioutil" + "net/http" + "net/url" + "strconv" + "strings" + "time" + + "github.com/markbates/goth" + "golang.org/x/oauth2" +) + +// These vars define the Authentication, Token, and API URLS for Azure. If +// using Azure enterprise you should change these values before calling New. +var ( + authURL = "https://app.vssps.visualstudio.com/oauth2/authorize" + tokenURL = "https://app.vssps.visualstudio.com/oauth2/token" + profileURL = "https://app.vssps.visualstudio.com/_apis/profile/profiles/me" +) + +// New creates a new Azure provider, and sets up important connection details. +// You should always call `azure.New` to get a new Provider. Never try to create +// one manually. +func New(clientKey, secret, callbackURL string, scopes ...string) *Provider { + return NewCustomisedURL(clientKey, secret, callbackURL, authURL, tokenURL, profileURL, scopes...) +} + +// NewCustomisedURL is similar to New(...) but can be used to set custom URLs to connect to +func NewCustomisedURL(clientKey, secret, callbackURL, authURL, tokenURL, profileURL string, scopes ...string) *Provider { + p := &Provider{ + ClientKey: clientKey, + Secret: secret, + CallbackURL: callbackURL, + providerName: "azuredevops", + profileURL: profileURL, + } + p.config = newConfig(p, authURL, tokenURL, scopes) + return p +} + +// Provider is the implementation of `goth.Provider` for accessing Azure. +type Provider struct { + ClientKey string + Secret string + CallbackURL string + HTTPClient *http.Client + config *oauth2.Config + providerName string + profileURL string +} + +// Name is the name used to retrieve this provider later. +func (p *Provider) Name() string { + return p.providerName +} + +// SetName is to update the name of the provider (needed in case of multiple providers of 1 type) +func (p *Provider) SetName(name string) { + p.providerName = name +} + +func (p *Provider) Client() *http.Client { + return goth.HTTPClientWithFallBack(p.HTTPClient) +} + +// Debug is a no-op for the azure package. +func (p *Provider) Debug(_ bool) {} + +// BeginAuth asks Azure for an authentication end-point. +func (p *Provider) BeginAuth(state string) (goth.Session, error) { + urlStr := p.config.AuthCodeURL(state) + session := &Session{ + AuthURL: urlStr, + } + return session, nil +} + +// FetchUser will go to Azure and access basic information about the user. +func (p *Provider) FetchUser(session goth.Session) (goth.User, error) { + sess := session.(*Session) + user := goth.User{ + AccessToken: sess.AccessToken, + RefreshToken: sess.RefreshToken, + ExpiresAt: sess.ExpiresAt, + Provider: p.Name(), + } + + if user.AccessToken == "" { + // data is not yet retrieved since accessToken is still empty + return user, fmt.Errorf("%s cannot get user information without accessToken", p.providerName) + } + + req, err := http.NewRequest("GET", p.profileURL, nil) + if err != nil { + return user, err + } + + req.Header.Add("Authorization", "Bearer "+sess.AccessToken) + response, err := p.Client().Do(req) + if err != nil { + return user, err + } + defer response.Body.Close() + + if response.StatusCode != http.StatusOK { + return user, fmt.Errorf("Azure Devops API responded with a %d trying to fetch user information", response.StatusCode) + } + + bits, err := ioutil.ReadAll(response.Body) + if err != nil { + return user, err + } + + err = json.NewDecoder(bytes.NewReader(bits)).Decode(&user.RawData) + if err != nil { + return user, err + } + + err = userFromReader(bytes.NewReader(bits), &user) + if err != nil { + return user, err + } + + return user, err +} + +func userFromReader(reader io.Reader, user *goth.User) error { + u := struct { + ID string `json:"id"` + Email string `json:"emailAddress"` + Name string `json:"displayName"` + }{} + + err := json.NewDecoder(reader).Decode(&u) + if err != nil { + return err + } + + user.Name = u.Name + user.Email = u.Email + user.UserID = u.ID + + return err +} + +func tokenFromReader(reader io.Reader, token *oauth2.Token) error { + t := struct { + AccessToken string `json:"access_token"` + RefreshToken string `json:"refresh_token"` + ExpiresIn string `json:"expires_in"` + }{} + + err := json.NewDecoder(reader).Decode(&t) + if err != nil { + return err + } + + expiresIn, err := strconv.Atoi(t.ExpiresIn) + if err != nil { + return err + } + token.AccessToken = t.AccessToken + token.RefreshToken = t.RefreshToken + token.Expiry = time.Now().Add(time.Duration(expiresIn) * time.Second) + + return err +} + +func newConfig(provider *Provider, authURL, tokenURL string, scopes []string) *oauth2.Config { + c := &oauth2.Config{ + ClientID: provider.ClientKey, + ClientSecret: provider.Secret, + RedirectURL: provider.CallbackURL, + Endpoint: oauth2.Endpoint{ + AuthURL: authURL, + TokenURL: tokenURL, + }, + Scopes: []string{}, + } + + for _, scope := range scopes { + c.Scopes = append(c.Scopes, scope) + } + + return c +} + +func (p *Provider) RefreshToken(refreshToken string) (*oauth2.Token, error) { + token := &oauth2.Token{ + RefreshToken: refreshToken, + } + + req, err := http.NewRequest("POST", p.config.Endpoint.TokenURL, nil) + if err != nil { + return token, err + } + + form := url.Values{} + form.Add("client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer") + form.Add("client_assertion", p.Secret) + form.Add("grant_type", "refresh_token") + form.Add("assertion", refreshToken) + form.Add("redirect_uri", p.config.RedirectURL) + + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + req.Body = ioutil.NopCloser(strings.NewReader(form.Encode())) + response, err := p.Client().Do(req) + + if err != nil { + return token, err + } + defer response.Body.Close() + + if response.StatusCode != http.StatusOK { + return token, fmt.Errorf("Azure Devops API responded with a %d trying to fetch user information", response.StatusCode) + } + + bits, err := ioutil.ReadAll(response.Body) + if err != nil { + return token, err + } + + err = tokenFromReader(bytes.NewReader(bits), token) + if err != nil { + return token, err + } + + return token, err +} + +func (p *Provider) RefreshTokenAvailable() bool { + return true +} diff --git a/providers/azuredevops/azuredevops_test.go b/providers/azuredevops/azuredevops_test.go new file mode 100644 index 000000000..545d96b00 --- /dev/null +++ b/providers/azuredevops/azuredevops_test.go @@ -0,0 +1,59 @@ +package azuredevops_test + +import ( + "fmt" + "os" + "testing" + + "github.com/markbates/goth" + "github.com/markbates/goth/providers/azuredevops" + "github.com/stretchr/testify/assert" +) + +func Test_New(t *testing.T) { + t.Parallel() + a := assert.New(t) + + provider := azureProvider() + a.Equal(provider.ClientKey, os.Getenv("AZUREDEVOPS_KEY")) + a.Equal(provider.Secret, os.Getenv("AZUREDEVOPS_SECRET")) + a.Equal(provider.CallbackURL, "/foo") +} + +func Test_Implements_Provider(t *testing.T) { + t.Parallel() + a := assert.New(t) + + a.Implements((*goth.Provider)(nil), azureProvider()) +} + +func Test_BeginAuth(t *testing.T) { + t.Parallel() + a := assert.New(t) + + provider := azureProvider() + session, err := provider.BeginAuth("test_state") + s := session.(*azuredevops.Session) + a.NoError(err) + a.Contains(s.AuthURL, "https://app.vssps.visualstudio.com/oauth2/authorize") + a.Contains(s.AuthURL, fmt.Sprintf("client_id=%s", os.Getenv("AZUREDEVOPS_KEY"))) + a.Contains(s.AuthURL, "state=test_state") + a.Contains(s.AuthURL, "scope=user") +} + +func Test_SessionFromJSON(t *testing.T) { + t.Parallel() + a := assert.New(t) + + provider := azureProvider() + + s, err := provider.UnmarshalSession(`{"AuthURL":"https://app.vssps.visualstudio.com/oauth2/authorize","AccessToken":"1234567890"}`) + a.NoError(err) + session := s.(*azuredevops.Session) + a.Equal(session.AuthURL, "https://app.vssps.visualstudio.com/oauth2/authorize") + a.Equal(session.AccessToken, "1234567890") +} + +func azureProvider() *azuredevops.Provider { + return azuredevops.New(os.Getenv("AZUREDEVOPS_KEY"), os.Getenv("AZUREDEVOPS_SECRET"), "/foo", "user") +} diff --git a/providers/azuredevops/session.go b/providers/azuredevops/session.go new file mode 100644 index 000000000..910900dd4 --- /dev/null +++ b/providers/azuredevops/session.go @@ -0,0 +1,125 @@ +package azuredevops + +import ( + "encoding/json" + "errors" + "fmt" + "io/ioutil" + "net/http" + "net/url" + "strconv" + "strings" + "time" + + "github.com/markbates/goth" +) + +type exchangeTokenResponse struct { + Error string `json:"Error"` + ErrorDescription string `json:"ErrorDescription"` + AccessToken string `json:"access_token"` + RefreshToken string `json:"refresh_token"` + TokenType string `json:"token_type"` + ExpiresIn string `json:"expires_in"` + Scopes string `json:"scope"` +} + +// Session stores data during the auth process with Azure. +type Session struct { + AuthURL string + AccessToken string + RefreshToken string + ExpiresAt time.Time +} + +// GetAuthURL will return the URL set by calling the `BeginAuth` function on the Azure provider. +func (s Session) GetAuthURL() (string, error) { + if s.AuthURL == "" { + return "", errors.New(goth.NoAuthUrlErrorMessage) + } + return s.AuthURL, nil +} + +// Authorize the session with Azure and return the access token to be stored for future use. +func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { + p := provider.(*Provider) + v := url.Values{ + "client_assertion_type": CondVal("urn:ietf:params:oauth:client-assertion-type:jwt-bearer"), + "grant_type": CondVal("urn:ietf:params:oauth:grant-type:jwt-bearer"), + "assertion": CondVal(params.Get("code")), + "redirect_uri": CondVal(p.config.RedirectURL), + } + + authData, err := retrieveAuthData(p, tokenURL, v) + if err != nil { + return "", err + } + + expiresIn, err := strconv.Atoi(authData.ExpiresIn) + if err != nil { + return "", err + } + + s.AccessToken = authData.AccessToken + s.RefreshToken = authData.RefreshToken + s.ExpiresAt = time.Now().Add(time.Duration(expiresIn) * time.Second) + return s.AccessToken, err +} + +// Marshal the session into a string +func (s Session) Marshal() string { + b, _ := json.Marshal(s) + return string(b) +} + +func (s Session) String() string { + return s.Marshal() +} + +// CondVal convert string in string array +func CondVal(v string) []string { + if v == "" { + return nil + } + return []string{v} +} + +// Custom implementation for azure to get access token and user data +// Azure provides user data along with access token, no separate api available +func retrieveAuthData(p *Provider, TokenURL string, v url.Values) (exchangeTokenResponse, error) { + v.Set("client_assertion", p.Secret) + req, err := http.NewRequest("POST", TokenURL, strings.NewReader(v.Encode())) + if err != nil { + return exchangeTokenResponse{}, err + } + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + + r, err := p.Client().Do(req) + if err != nil { + return exchangeTokenResponse{}, err + } + defer r.Body.Close() + body, err := ioutil.ReadAll(r.Body) + if err != nil { + return exchangeTokenResponse{}, fmt.Errorf("oauth2: cannot fetch token: %v", err) + } + if code := r.StatusCode; code < 200 || code > 299 { + return exchangeTokenResponse{}, fmt.Errorf("oauth2: cannot fetch token: %v\nResponse: %s", r.Status, body) + } + + var response exchangeTokenResponse + + err = json.Unmarshal(body, &response) + + if err != nil { + return exchangeTokenResponse{}, err + } + return response, nil +} + +// UnmarshalSession will unmarshal a JSON string into a session. +func (p *Provider) UnmarshalSession(data string) (goth.Session, error) { + sess := &Session{} + err := json.NewDecoder(strings.NewReader(data)).Decode(sess) + return sess, err +} diff --git a/providers/azuredevops/session_test.go b/providers/azuredevops/session_test.go new file mode 100644 index 000000000..edadfc865 --- /dev/null +++ b/providers/azuredevops/session_test.go @@ -0,0 +1,48 @@ +package azuredevops_test + +import ( + "testing" + + "github.com/markbates/goth" + "github.com/markbates/goth/providers/azuredevops" + "github.com/stretchr/testify/assert" +) + +func Test_Implements_Session(t *testing.T) { + t.Parallel() + a := assert.New(t) + s := &azuredevops.Session{} + + a.Implements((*goth.Session)(nil), s) +} + +func Test_GetAuthURL(t *testing.T) { + t.Parallel() + a := assert.New(t) + s := &azuredevops.Session{} + + _, err := s.GetAuthURL() + a.Error(err) + + s.AuthURL = "/foo" + + url, _ := s.GetAuthURL() + a.Equal(url, "/foo") +} + +func Test_ToJSON(t *testing.T) { + t.Parallel() + a := assert.New(t) + s := &azuredevops.Session{} + + data := s.Marshal() + a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) +} + +func Test_String(t *testing.T) { + t.Parallel() + a := assert.New(t) + s := &azuredevops.Session{} + + a.Equal(s.String(), s.Marshal()) +}