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
ef3beb1f
Commit
ef3beb1f
authored
Sep 04, 2018
by
Erik Sundell
Committed by
Daniel Lee
Sep 14, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adds poc code for retrieving google auth accesstoken
parent
834d06c3
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
56 additions
and
40 deletions
+56
-40
pkg/api/pluginproxy/ds_proxy.go
+56
-40
No files found.
pkg/api/pluginproxy/ds_proxy.go
View file @
ef3beb1f
...
...
@@ -2,7 +2,7 @@ package pluginproxy
import
(
"bytes"
"
encoding/json
"
"
context
"
"errors"
"fmt"
"io/ioutil"
...
...
@@ -22,6 +22,7 @@ import (
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util"
"golang.org/x/oauth2/jwt"
)
var
(
...
...
@@ -353,49 +354,64 @@ func (proxy *DataSourceProxy) applyRoute(req *http.Request) {
}
func
(
proxy
*
DataSourceProxy
)
getAccessToken
(
data
templateData
)
(
string
,
error
)
{
if
cachedToken
,
found
:=
tokenCache
[
proxy
.
getAccessTokenCacheKey
()];
found
{
if
cachedToken
.
ExpiresOn
.
After
(
time
.
Now
()
.
Add
(
time
.
Second
*
10
))
{
logger
.
Info
(
"Using token from cache"
)
return
cachedToken
.
AccessToken
,
nil
}
conf
:=
jwt
.
Config
{
Email
:
"raintank-production-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fy7S54OZHdhoUpTc
\n
Z+qWTSG6QgjHDzNaqZ+p6ehDCnO9EyuwpYHXcFIavlmSxszUPNy6f3TosvKcvm6q
\n
5CFzdt4fgev3cgGB+P1mT1gyi6UjntWuAj1fFvxh+o87kq9v4p6YVX+woll10CaH
\n
++O3QNlpLwKBgQCRV5qM0by26M8MJVwtSkaxdxrfsjdfv9zeibnY2Y5dSwB9Xvqs
\n
QcF5sHNNxMGIOeefZ/C/EgAW5yKbxg+bHqqmXjxi1sZtnS2CozuXW+Iz5zccbOnL
\n
wRyMVPrCujsggvaGIHxgBj+a1kJ0Hy/yfe+guwS6APZditbIIAACRWmADwKBgAuA
\n
HC32ZOaxKN/Sg+xsMpSYHe0dlZylxOoM6t573GSeRb1YjHBNqcX86pl/xMEyt7w6
\n
DF8+c1uGCDq+b2ugfHZ6BOGQfNKQYn/rvMhX0mVSxT6SrtVMizIYK/q4AoK8E7rE
\n
GNwXqYbM8qlY692fzwrYBR8Md1KCpGI+nF9+gAOxAoGAAwJO+jr44SRldSSsli0d
\n
DbGRmlMc093MebjwNsO2NGoF8uTRyYIzchP2l57PMPKFX/r5IcchjVh3wHWwtzMS
\n
hB8zfRDj7RFjW0H8U1Qf5k2ID3ACJtxnt+o744Lggqzf9puf4RKwwAjIwJy2lhbi
\n
yA+63fAHMAwG+k22IkBqcu4=
\n
-----END PRIVATE KEY-----
\n
"
),
Scopes
:
[]
string
{
"https://www.googleapis.com/auth/monitoring.read"
},
TokenURL
:
"https://oauth2.googleapis.com/token"
,
}
urlInterpolated
,
err
:=
interpolateString
(
proxy
.
route
.
TokenAuth
.
Url
,
data
)
ctx
:=
context
.
Background
()
tokenSrc
:=
conf
.
TokenSource
(
ctx
)
// logger.Info("Accesstoken", tokenSrc.Token.AccessToken)
token
,
err
:=
tokenSrc
.
Token
()
if
err
!=
nil
{
return
""
,
err
}
params
:=
make
(
url
.
Values
)
for
key
,
value
:=
range
proxy
.
route
.
TokenAuth
.
Params
{
interpolatedParam
,
err
:=
interpolateString
(
value
,
data
)
if
err
!=
nil
{
return
""
,
err
}
params
.
Add
(
key
,
interpolatedParam
)
logger
.
Info
(
"GetToken"
,
"Error"
,
err
)
}
getTokenReq
,
_
:=
http
.
NewRequest
(
"POST"
,
urlInterpolated
,
bytes
.
NewBufferString
(
params
.
Encode
()))
getTokenReq
.
Header
.
Add
(
"Content-Type"
,
"application/x-www-form-urlencoded"
)
getTokenReq
.
Header
.
Add
(
"Content-Length"
,
strconv
.
Itoa
(
len
(
params
.
Encode
())))
resp
,
err
:=
client
.
Do
(
getTokenReq
)
if
err
!=
nil
{
return
""
,
err
}
defer
resp
.
Body
.
Close
()
var
token
jwtToken
if
err
:=
json
.
NewDecoder
(
resp
.
Body
)
.
Decode
(
&
token
);
err
!=
nil
{
return
""
,
err
}
expiresOnEpoch
,
_
:=
strconv
.
ParseInt
(
token
.
ExpiresOnString
,
10
,
64
)
token
.
ExpiresOn
=
time
.
Unix
(
expiresOnEpoch
,
0
)
tokenCache
[
proxy
.
getAccessTokenCacheKey
()]
=
&
token
logger
.
Info
(
"Got new access token"
,
"ExpiresOn"
,
token
.
ExpiresOn
)
logger
.
Info
(
"GetToken"
,
"Token"
,
token
.
AccessToken
)
return
token
.
AccessToken
,
nil
// if cachedToken, found := tokenCache[proxy.getAccessTokenCacheKey()]; found {
// if cachedToken.ExpiresOn.After(time.Now().Add(time.Second * 10)) {
// logger.Info("Using token from cache")
// return cachedToken.AccessToken, nil
// }
// }
// urlInterpolated, err := interpolateString(proxy.route.TokenAuth.Url, data)
// if err != nil {
// return "", err
// }
// params := make(url.Values)
// for key, value := range proxy.route.TokenAuth.Params {
// interpolatedParam, err := interpolateString(value, data)
// if err != nil {
// return "", err
// }
// params.Add(key, interpolatedParam)
// }
// getTokenReq, _ := http.NewRequest("POST", urlInterpolated, bytes.NewBufferString(params.Encode()))
// getTokenReq.Header.Add("Content-Type", "application/x-www-form-urlencoded")
// getTokenReq.Header.Add("Content-Length", strconv.Itoa(len(params.Encode())))
// resp, err := client.Do(getTokenReq)
// if err != nil {
// return "", err
// }
// defer resp.Body.Close()
// var token jwtToken
// if err := json.NewDecoder(resp.Body).Decode(&token); err != nil {
// return "", err
// }
// expiresOnEpoch, _ := strconv.ParseInt(token.ExpiresOnString, 10, 64)
// token.ExpiresOn = time.Unix(expiresOnEpoch, 0)
// tokenCache[proxy.getAccessTokenCacheKey()] = &token
// logger.Info("Got new access token", "ExpiresOn", token.ExpiresOn)
// return token.AccessToken, nil
}
func
(
proxy
*
DataSourceProxy
)
getAccessTokenCacheKey
()
string
{
...
...
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