File tree Expand file tree Collapse file tree 2 files changed +20
-1
lines changed Expand file tree Collapse file tree 2 files changed +20
-1
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,8 @@ package api
3
3
import (
4
4
"errors"
5
5
"fmt"
6
+ "crypto/rand"
7
+ "encoding/base64"
6
8
7
9
"golang.org/x/oauth2"
8
10
@@ -14,6 +16,12 @@ import (
14
16
"github.com/grafana/grafana/pkg/social"
15
17
)
16
18
19
+ func GenStateString () string {
20
+ rnd := make ([]byte , 32 )
21
+ rand .Read (rnd )
22
+ return base64 .StdEncoding .EncodeToString (rnd )
23
+ }
24
+
17
25
func OAuthLogin (ctx * middleware.Context ) {
18
26
if setting .OAuthService == nil {
19
27
ctx .Handle (404 , "login.OAuthLogin(oauth service not enabled)" , nil )
@@ -29,7 +37,17 @@ func OAuthLogin(ctx *middleware.Context) {
29
37
30
38
code := ctx .Query ("code" )
31
39
if code == "" {
32
- ctx .Redirect (connect .AuthCodeURL ("" , oauth2 .AccessTypeOnline ))
40
+ state := GenStateString ()
41
+ ctx .Session .Set (middleware .SESS_KEY_OAUTH_STATE , state )
42
+ ctx .Redirect (connect .AuthCodeURL (state , oauth2 .AccessTypeOnline ))
43
+ return
44
+ }
45
+
46
+ // verify state string
47
+ savedState := ctx .Session .Get (middleware .SESS_KEY_OAUTH_STATE ).(string )
48
+ queryState := ctx .Query ("state" )
49
+ if savedState != queryState {
50
+ ctx .Handle (500 , "login.OAuthLogin(state mismatch)" , nil )
33
51
return
34
52
}
35
53
Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ import (
13
13
14
14
const (
15
15
SESS_KEY_USERID = "uid"
16
+ SESS_KEY_OAUTH_STATE = "state"
16
17
)
17
18
18
19
var sessionManager * session.Manager
You can’t perform that action at this time.
0 commit comments