Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
3bba8b2c
Commit
3bba8b2c
authored
Sep 22, 2014
by
Torkel Ödegaard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Github oauth login works
parent
b0b77d66
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
142 additions
and
20 deletions
+142
-20
grafana
+1
-1
pkg/api/api_oauth.go
+1
-0
pkg/api/api_oauth_github.go
+112
-0
pkg/api/api_oauth_google.go
+18
-16
pkg/configuration/configuration.go
+9
-3
pkg/models/account.go
+1
-0
No files found.
grafana
@
d584076b
Subproject commit
071ac0dc85e48be546315dde196f90f01ad7b274
Subproject commit
d584076b93b4ebfb33e5a5f375feb6d6ff7f9bfc
pkg/api/api_oauth.go
0 → 100644
View file @
3bba8b2c
package
api
pkg/api/api_oauth_github.go
0 → 100644
View file @
3bba8b2c
package
api
import
(
"encoding/json"
"net/http"
log
"github.com/alecthomas/log4go"
"github.com/gin-gonic/gin"
"github.com/golang/oauth2"
"github.com/torkelo/grafana-pro/pkg/models"
"github.com/torkelo/grafana-pro/pkg/stores"
)
var
(
githubOAuthConfig
*
oauth2
.
Config
githubRedirectUrl
string
=
"http://localhost:3000/oauth2/github/callback"
githubAuthUrl
string
=
"https://github.com/login/oauth/authorize"
githubTokenUrl
string
=
"https://github.com/login/oauth/access_token"
)
func
init
()
{
addRoutes
(
func
(
self
*
HttpServer
)
{
if
!
self
.
cfg
.
Http
.
GithubOAuth
.
Enabled
{
return
}
self
.
router
.
GET
(
"/oauth2/github"
,
self
.
oauthGithub
)
self
.
router
.
GET
(
"/oauth2/github/callback"
,
self
.
oauthGithubCallback
)
options
:=
&
oauth2
.
Options
{
ClientID
:
self
.
cfg
.
Http
.
GithubOAuth
.
ClientId
,
ClientSecret
:
self
.
cfg
.
Http
.
GithubOAuth
.
ClientSecret
,
RedirectURL
:
githubRedirectUrl
,
Scopes
:
[]
string
{
"user:email"
},
}
cfg
,
err
:=
oauth2
.
NewConfig
(
options
,
githubAuthUrl
,
githubTokenUrl
)
if
err
!=
nil
{
log
.
Error
(
"Failed to init github auth %v"
,
err
)
}
githubOAuthConfig
=
cfg
})
}
func
(
self
*
HttpServer
)
oauthGithub
(
c
*
gin
.
Context
)
{
url
:=
githubOAuthConfig
.
AuthCodeURL
(
""
,
"online"
,
"auto"
)
c
.
Redirect
(
302
,
url
)
}
type
githubUserInfoDto
struct
{
Login
string
`json:"login"`
Name
string
`json:"name"`
Email
string
`json:"email"`
Company
string
`json:"company"`
}
func
(
self
*
HttpServer
)
oauthGithubCallback
(
c
*
gin
.
Context
)
{
code
:=
c
.
Request
.
URL
.
Query
()[
"code"
][
0
]
log
.
Info
(
"OAuth code: %v"
,
code
)
transport
,
err
:=
githubOAuthConfig
.
NewTransportWithCode
(
code
)
if
err
!=
nil
{
c
.
String
(
500
,
"Failed to exchange oauth token: "
+
err
.
Error
())
return
}
client
:=
http
.
Client
{
Transport
:
transport
}
resp
,
err
:=
client
.
Get
(
"https://api.github.com/user"
)
if
err
!=
nil
{
c
.
String
(
500
,
err
.
Error
())
return
}
var
userInfo
githubUserInfoDto
decoder
:=
json
.
NewDecoder
(
resp
.
Body
)
err
=
decoder
.
Decode
(
&
userInfo
)
if
err
!=
nil
{
c
.
String
(
500
,
err
.
Error
())
return
}
if
len
(
userInfo
.
Email
)
<
5
{
c
.
String
(
500
,
"Invalid email"
)
return
}
// try find existing account
account
,
err
:=
self
.
store
.
GetAccountByLogin
(
userInfo
.
Email
)
// create account if missing
if
err
==
stores
.
ErrAccountNotFound
{
account
=
&
models
.
Account
{
Login
:
userInfo
.
Login
,
Email
:
userInfo
.
Email
,
Name
:
userInfo
.
Name
,
Company
:
userInfo
.
Company
,
}
if
err
=
self
.
store
.
CreateAccount
(
account
);
err
!=
nil
{
log
.
Error
(
"Failed to create account %v"
,
err
)
c
.
String
(
500
,
"Failed to create account"
)
return
}
}
// login
loginUserWithAccount
(
account
,
c
)
c
.
Redirect
(
302
,
"/"
)
}
pkg/api/api_
google_oauth
.go
→
pkg/api/api_
oauth_google
.go
View file @
3bba8b2c
...
@@ -11,7 +11,14 @@ import (
...
@@ -11,7 +11,14 @@ import (
"github.com/torkelo/grafana-pro/pkg/stores"
"github.com/torkelo/grafana-pro/pkg/stores"
)
)
var
oauthCfg
*
oauth2
.
Config
var
(
googleOAuthConfig
*
oauth2
.
Config
googleRedirectUrl
string
=
"http://localhost:3000/oauth2/google/callback"
googleAuthUrl
string
=
"https://accounts.google.com/o/oauth2/auth"
googleTokenUrl
string
=
"https://accounts.google.com/o/oauth2/token"
googleScopeProfile
string
=
"https://www.googleapis.com/auth/userinfo.profile"
googleScopeEmail
string
=
"https://www.googleapis.com/auth/userinfo.email"
)
func
init
()
{
func
init
()
{
addRoutes
(
func
(
self
*
HttpServer
)
{
addRoutes
(
func
(
self
*
HttpServer
)
{
...
@@ -19,33 +26,28 @@ func init() {
...
@@ -19,33 +26,28 @@ func init() {
return
return
}
}
self
.
router
.
GET
(
"/
login/google"
,
self
.
login
Google
)
self
.
router
.
GET
(
"/
oauth2/google"
,
self
.
oauth
Google
)
self
.
router
.
GET
(
"/oauth2
callback"
,
self
.
oauth
Callback
)
self
.
router
.
GET
(
"/oauth2
/google/callback"
,
self
.
oauthGoogle
Callback
)
options
:=
&
oauth2
.
Options
{
options
:=
&
oauth2
.
Options
{
ClientID
:
self
.
cfg
.
Http
.
GoogleOAuth
.
ClientId
,
ClientID
:
self
.
cfg
.
Http
.
GoogleOAuth
.
ClientId
,
ClientSecret
:
self
.
cfg
.
Http
.
GoogleOAuth
.
ClientSecret
,
ClientSecret
:
self
.
cfg
.
Http
.
GoogleOAuth
.
ClientSecret
,
RedirectURL
:
"http://localhost:3000/oauth2callback"
,
RedirectURL
:
googleRedirectUrl
,
Scopes
:
[]
string
{
Scopes
:
[]
string
{
googleScopeEmail
,
googleScopeProfile
},
"https://www.googleapis.com/auth/userinfo.profile"
,
"https://www.googleapis.com/auth/userinfo.email"
,
},
}
}
cfg
,
err
:=
oauth2
.
NewConfig
(
options
,
cfg
,
err
:=
oauth2
.
NewConfig
(
options
,
googleAuthUrl
,
googleTokenUrl
)
"https://accounts.google.com/o/oauth2/auth"
,
"https://accounts.google.com/o/oauth2/token"
)
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Error
(
"Failed to init google auth %v"
,
err
)
log
.
Error
(
"Failed to init google auth %v"
,
err
)
}
}
oauthCf
g
=
cfg
googleOAuthConfi
g
=
cfg
})
})
}
}
func
(
self
*
HttpServer
)
login
Google
(
c
*
gin
.
Context
)
{
func
(
self
*
HttpServer
)
oauth
Google
(
c
*
gin
.
Context
)
{
url
:=
oauthCf
g
.
AuthCodeURL
(
""
,
"online"
,
"auto"
)
url
:=
googleOAuthConfi
g
.
AuthCodeURL
(
""
,
"online"
,
"auto"
)
c
.
Redirect
(
302
,
url
)
c
.
Redirect
(
302
,
url
)
}
}
...
@@ -56,11 +58,11 @@ type googleUserInfoDto struct {
...
@@ -56,11 +58,11 @@ type googleUserInfoDto struct {
Name
string
`json:"name"`
Name
string
`json:"name"`
}
}
func
(
self
*
HttpServer
)
oauthCallback
(
c
*
gin
.
Context
)
{
func
(
self
*
HttpServer
)
oauth
Google
Callback
(
c
*
gin
.
Context
)
{
code
:=
c
.
Request
.
URL
.
Query
()[
"code"
][
0
]
code
:=
c
.
Request
.
URL
.
Query
()[
"code"
][
0
]
log
.
Info
(
"OAuth code: %v"
,
code
)
log
.
Info
(
"OAuth code: %v"
,
code
)
transport
,
err
:=
oauthCf
g
.
NewTransportWithCode
(
code
)
transport
,
err
:=
googleOAuthConfi
g
.
NewTransportWithCode
(
code
)
if
err
!=
nil
{
if
err
!=
nil
{
c
.
String
(
500
,
"Failed to exchange oauth token: "
+
err
.
Error
())
c
.
String
(
500
,
"Failed to exchange oauth token: "
+
err
.
Error
())
return
return
...
...
pkg/configuration/configuration.go
View file @
3bba8b2c
...
@@ -6,10 +6,11 @@ type Cfg struct {
...
@@ -6,10 +6,11 @@ type Cfg struct {
type
HttpCfg
struct
{
type
HttpCfg
struct
{
Port
string
Port
string
GoogleOAuth
GoogleOAuthCfg
GoogleOAuth
OAuthCfg
GithubOAuth
OAuthCfg
}
}
type
Google
OAuthCfg
struct
{
type
OAuthCfg
struct
{
Enabled
bool
Enabled
bool
ClientId
string
ClientId
string
ClientSecret
string
ClientSecret
string
...
@@ -24,11 +25,16 @@ func NewCfg(port string) *Cfg {
...
@@ -24,11 +25,16 @@ func NewCfg(port string) *Cfg {
return
&
Cfg
{
return
&
Cfg
{
Http
:
HttpCfg
{
Http
:
HttpCfg
{
Port
:
port
,
Port
:
port
,
GoogleOAuth
:
Google
OAuthCfg
{
GoogleOAuth
:
OAuthCfg
{
Enabled
:
true
,
Enabled
:
true
,
ClientId
:
"106011922963-4pvl05e9urtrm8bbqr0vouosj3e8p8kb.apps.googleusercontent.com"
,
ClientId
:
"106011922963-4pvl05e9urtrm8bbqr0vouosj3e8p8kb.apps.googleusercontent.com"
,
ClientSecret
:
"K2evIa4QhfbhhAm3SO72t2Zv"
,
ClientSecret
:
"K2evIa4QhfbhhAm3SO72t2Zv"
,
},
},
GithubOAuth
:
OAuthCfg
{
Enabled
:
true
,
ClientId
:
"de054205006b9baa2e17"
,
ClientSecret
:
"72b7ea52d9f1096fdf36cea95e95362a307e0322"
,
},
},
},
}
}
}
}
pkg/models/account.go
View file @
3bba8b2c
...
@@ -27,6 +27,7 @@ type Account struct {
...
@@ -27,6 +27,7 @@ type Account struct {
AccountName
string
AccountName
string
Password
string
Password
string
Name
string
Name
string
Company
string
NextDashboardId
int
NextDashboardId
int
UsingAccountId
int
UsingAccountId
int
Collaborators
[]
CollaboratorLink
Collaborators
[]
CollaboratorLink
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment