feat(v2): use Token authentication for InfluxDB v2

pull/5619/head
Pavel Zavora 2020-11-24 19:58:48 +01:00
parent f4db32d8a1
commit 6bb9afc296
1 changed files with 33 additions and 10 deletions

View File

@ -3,6 +3,7 @@ package influx
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"strings"
"time" "time"
jwt "github.com/dgrijalva/jwt-go" jwt "github.com/dgrijalva/jwt-go"
@ -21,20 +22,30 @@ type NoAuthorization struct{}
// Set does not add authorization // Set does not add authorization
func (n *NoAuthorization) Set(req *http.Request) error { return nil } func (n *NoAuthorization) Set(req *http.Request) error { return nil }
// DefaultAuthorization creates either a shared JWT builder, basic auth or Noop // DefaultAuthorization creates either a shared JWT builder, basic auth or Noop or Token authentication
func DefaultAuthorization(src *chronograf.Source) Authorizer { func DefaultAuthorization(src *chronograf.Source) Authorizer {
// Token authentication for InfluxDB v2
if (src.Version == "" || strings.HasPrefix(src.Version, "2.")) && src.Password != "" {
return &TokenAuth{
Token: src.Password,
}
}
if src.Username != "" {
// Optionally, add the shared secret JWT token creation // Optionally, add the shared secret JWT token creation
if src.Username != "" && src.SharedSecret != "" { if src.SharedSecret != "" {
return &BearerJWT{ return &BearerJWT{
Username: src.Username, Username: src.Username,
SharedSecret: src.SharedSecret, SharedSecret: src.SharedSecret,
} }
} else if src.Username != "" && src.Password != "" { }
// use standard basic authentication
if src.Password != "" {
return &BasicAuth{ return &BasicAuth{
Username: src.Username, Username: src.Username,
Password: src.Password, Password: src.Password,
} }
} }
}
return &NoAuthorization{} return &NoAuthorization{}
} }
@ -50,6 +61,18 @@ func (b *BasicAuth) Set(r *http.Request) error {
return nil return nil
} }
// TokenAuth adds Authorization: Token to the request header
type TokenAuth struct {
Token string
}
// Set adds the token authentication to the request
func (a *TokenAuth) Set(r *http.Request) error {
fmt.Println("setting up token " + a.Token)
r.Header.Set("Authorization", "Token "+a.Token)
return nil
}
// BearerJWT is the default Bearer for InfluxDB // BearerJWT is the default Bearer for InfluxDB
type BearerJWT struct { type BearerJWT struct {
Username string Username string