Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement oauth2 state correctly #32

Merged
merged 1 commit into from
Jan 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion server/cmd/code-annotation/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,7 @@ func main() {
})

logrus.Info("running...")
http.ListenAndServe(fmt.Sprintf("%s:%d", conf.Host, conf.Port), r)
if err := http.ListenAndServe(fmt.Sprintf("%s:%d", conf.Host, conf.Port), r); err != nil {
panic(err)
}
}
4 changes: 2 additions & 2 deletions server/handler/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import (
// Login handler redirects user to oauth provider
func Login(oAuth *service.OAuth) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
url := oAuth.MakeAuthURL()
url := oAuth.MakeAuthURL(w, r)
http.Redirect(w, r, url, http.StatusTemporaryRedirect)
}
}

// OAuthCallback makes exchange with oauth provider, gets&creates user and redirects to index page with JWT token
func OAuthCallback(oAuth *service.OAuth, jwt *service.JWT, userRepo *repository.Users, uiDomain string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if err := oAuth.ValidateState(r.FormValue("state")); err != nil {
if err := oAuth.ValidateState(r, r.FormValue("state")); err != nil {
writeResponse(w, respErr(http.StatusBadRequest, err.Error()))
return
}
Expand Down
31 changes: 22 additions & 9 deletions server/service/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@ package service

import (
"context"
"crypto/rand"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"

"github.com/gorilla/sessions"
"github.com/src-d/code-annotation/server/model"
"golang.org/x/oauth2"
"golang.org/x/oauth2/github"
)

// State is a token to protect the user from CSRF attacks.
// You must always provide a non-empty string and validate that it matches the the state query parameter on your redirect callback
// FIXME issue: https://github.com/src-d/code-annotation/issues/21
const oauthStateString = "state"

// OAuthConfig defines enviroment variables for OAuth
type OAuthConfig struct {
ClientID string `envconfig:"CLIENT_ID" required:"true"`
Expand All @@ -24,6 +23,7 @@ type OAuthConfig struct {
// OAuth service abstracts OAuth implementation
type OAuth struct {
config *oauth2.Config
store *sessions.CookieStore
}

// NewOAuth return new OAuth service
Expand All @@ -36,6 +36,7 @@ func NewOAuth(clientID, clientSecret string) *OAuth {
}
return &OAuth{
config: config,
store: sessions.NewCookieStore([]byte(clientSecret)),
}
}

Expand All @@ -47,13 +48,25 @@ type githubUser struct {
}

// MakeAuthURL returns string for redirect to provider
func (o *OAuth) MakeAuthURL() string {
return o.config.AuthCodeURL(oauthStateString)
func (o *OAuth) MakeAuthURL(w http.ResponseWriter, r *http.Request) string {
b := make([]byte, 16)
rand.Read(b)
state := base64.URLEncoding.EncodeToString(b)

session, _ := o.store.Get(r, "sess")
session.Values["state"] = state
session.Save(r, w)

return o.config.AuthCodeURL(state)
}

// ValidateState protects the user from CSRF attacks
func (o *OAuth) ValidateState(state string) error {
if state != oauthStateString {
func (o *OAuth) ValidateState(r *http.Request, state string) error {
session, err := o.store.Get(r, "sess")
if err != nil {
return fmt.Errorf("can't get session: %s", err)
}
if state != session.Values["state"] {
return fmt.Errorf("incorrect state: %s", state)
}
return nil
Expand Down