@@ -14,28 +14,22 @@ import (
1414type authController struct {
1515 jwtSecret string
1616 tokenDurationSeconds int
17- username string
18- password string
17+ authenticator authenticator
1918}
2019
2120// GET /auth/token
2221// Requires basic auth
2322func (a authController ) getAuthToken (w http.ResponseWriter , r * http.Request ) {
2423 reqUsername , reqPassword , _ := r .BasicAuth ()
25- if reqUsername != a .username {
26- log .Printf ("Invalid username: %s" , reqUsername )
27- w .WriteHeader (http .StatusForbidden )
28- return
29- }
30- if reqPassword != a .password {
31- log .Printf ("Invalid password" )
24+ valid , err := a .authenticator .authenticate (reqUsername , reqPassword )
25+ if ! valid {
3226 w .WriteHeader (http .StatusForbidden )
3327 return
3428 }
3529
3630 token := jwt .NewWithClaims (jwt .SigningMethodHS256 , jwt.MapClaims {
3731 "subject" : "go" ,
38- "username" : a . username ,
32+ "username" : reqUsername ,
3933 "exp" : time .Now ().Unix () + int64 (a .tokenDurationSeconds ),
4034 })
4135 tokenString , err := token .SignedString ([]byte (a .jwtSecret ))
@@ -60,34 +54,50 @@ func (a authController) getAuthToken(w http.ResponseWriter, r *http.Request) {
6054 w .Write (httpJson )
6155}
6256
63- func tokenAuthMiddleware (jwtSecret string , next http.Handler ) http.Handler {
57+ func authMiddleware (jwtSecret string , apiKey string , next http.Handler ) http.Handler {
6458 return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
65- var tokenString string
59+ var bearerTokenString string
60+ var apiKeyString string
6661 for _ , headerValue := range r .Header ["Authorization" ] {
6762 if strings .HasPrefix (headerValue , "Bearer " ) {
68- tokenString , _ = strings .CutPrefix (headerValue , "Bearer " )
63+ bearerTokenString , _ = strings .CutPrefix (headerValue , "Bearer " )
64+ }
65+ if strings .HasPrefix (headerValue , "ApiKey " ) {
66+ apiKeyString , _ = strings .CutPrefix (headerValue , "ApiKey " )
6967 }
7068 }
7169
72- token , err := jwt .Parse (tokenString , func (token * jwt.Token ) (interface {}, error ) {
73- if _ , ok := token .Method .(* jwt.SigningMethodHMAC ); ! ok {
74- return nil , fmt .Errorf ("unexpected signing method: %v" , token .Header ["alg" ])
70+ if bearerTokenString != "" {
71+ token , err := jwt .Parse (bearerTokenString , func (token * jwt.Token ) (interface {}, error ) {
72+ if _ , ok := token .Method .(* jwt.SigningMethodHMAC ); ! ok {
73+ return nil , fmt .Errorf ("unexpected signing method: %v" , token .Header ["alg" ])
74+ }
75+ return []byte (jwtSecret ), nil
76+ })
77+ if err != nil {
78+ log .Printf ("JWT parsing failed: %s" , err )
79+ w .WriteHeader (http .StatusUnauthorized )
80+ return
7581 }
76- return []byte (jwtSecret ), nil
77- })
78- if err != nil {
79- log .Printf ("JWT parsing failed: %s" , err )
80- w .WriteHeader (http .StatusUnauthorized )
81- return
82- }
8382
84- claims , ok := token .Claims .(jwt.MapClaims )
85- if ! ok {
86- log .Printf ("JWT claims failed: %s" , err )
87- }
88- exp , _ := claims .GetExpirationTime ()
89- if exp .Before (time .Now ()) {
90- log .Printf ("JWT expired at: %s" , exp )
83+ claims , ok := token .Claims .(jwt.MapClaims )
84+ if ! ok {
85+ log .Printf ("JWT claims failed: %s" , err )
86+ }
87+ exp , _ := claims .GetExpirationTime ()
88+ if exp .Before (time .Now ()) {
89+ log .Printf ("JWT expired at: %s" , exp )
90+ w .WriteHeader (http .StatusUnauthorized )
91+ return
92+ }
93+ } else if apiKey != "" && apiKeyString != "" {
94+ if apiKeyString != apiKey {
95+ log .Printf ("Invalid API key" )
96+ w .WriteHeader (http .StatusUnauthorized )
97+ return
98+ }
99+ } else {
100+ log .Printf ("Authorization scheme not supported" )
91101 w .WriteHeader (http .StatusUnauthorized )
92102 return
93103 }
0 commit comments