Commit 81443bf8 by Eric Perrino

Added a state parameter for all OAuth requests

parent 913f17ee
...@@ -3,6 +3,8 @@ package api ...@@ -3,6 +3,8 @@ package api
import ( import (
"errors" "errors"
"fmt" "fmt"
"crypto/rand"
"encoding/base64"
"golang.org/x/oauth2" "golang.org/x/oauth2"
...@@ -14,6 +16,12 @@ import ( ...@@ -14,6 +16,12 @@ import (
"github.com/grafana/grafana/pkg/social" "github.com/grafana/grafana/pkg/social"
) )
func GenStateString() string {
rnd := make([]byte, 32)
rand.Read(rnd)
return base64.StdEncoding.EncodeToString(rnd)
}
func OAuthLogin(ctx *middleware.Context) { func OAuthLogin(ctx *middleware.Context) {
if setting.OAuthService == nil { if setting.OAuthService == nil {
ctx.Handle(404, "login.OAuthLogin(oauth service not enabled)", nil) ctx.Handle(404, "login.OAuthLogin(oauth service not enabled)", nil)
...@@ -29,7 +37,17 @@ func OAuthLogin(ctx *middleware.Context) { ...@@ -29,7 +37,17 @@ func OAuthLogin(ctx *middleware.Context) {
code := ctx.Query("code") code := ctx.Query("code")
if code == "" { if code == "" {
ctx.Redirect(connect.AuthCodeURL("", oauth2.AccessTypeOnline)) state := GenStateString()
ctx.Session.Set(middleware.SESS_KEY_OAUTH_STATE, state)
ctx.Redirect(connect.AuthCodeURL(state, oauth2.AccessTypeOnline))
return
}
// verify state string
savedState := ctx.Session.Get(middleware.SESS_KEY_OAUTH_STATE).(string)
queryState := ctx.Query("state")
if savedState != queryState {
ctx.Handle(500, "login.OAuthLogin(state mismatch)", nil)
return return
} }
......
...@@ -13,6 +13,7 @@ import ( ...@@ -13,6 +13,7 @@ import (
const ( const (
SESS_KEY_USERID = "uid" SESS_KEY_USERID = "uid"
SESS_KEY_OAUTH_STATE = "state"
) )
var sessionManager *session.Manager var sessionManager *session.Manager
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment