-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #269 from pace/client_id_header
Client id header
- Loading branch information
Showing
26 changed files
with
1,720 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright © 2021 by PACE Telematics GmbH. All rights reserved. | ||
// Created at 2021/05/10 by Alessandro Miceli | ||
|
||
package middleware | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"strings" | ||
|
||
jwt "github.com/dgrijalva/jwt-go" | ||
) | ||
|
||
// ClientIDHeaderName name of the HTTP header that is used for reporting | ||
const ( | ||
ClientIDHeaderName = "Client-ID" | ||
) | ||
|
||
var ErrEmptyAuthorizedParty = errors.New("authorized party is empty") | ||
|
||
func ClientID(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
value := r.Header.Get("Authorization") | ||
if strings.HasPrefix(value, "Bearer ") { | ||
var claim clientIDClaim | ||
|
||
_, _, err := new(jwt.Parser).ParseUnverified(value[7:], &claim) | ||
if err == nil { | ||
w.Header().Add(ClientIDHeaderName, claim.AuthorizedParty) | ||
} | ||
} | ||
next.ServeHTTP(w, r) | ||
}) | ||
} | ||
|
||
type clientIDClaim struct { | ||
AuthorizedParty string `json:"azp"` | ||
} | ||
|
||
func (c clientIDClaim) Valid() error { | ||
if c.AuthorizedParty == "" { | ||
return ErrEmptyAuthorizedParty | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright © 2021 by PACE Telematics GmbH. All rights reserved. | ||
// Created at 2021/05/10 by Alessandro Miceli | ||
|
||
package middleware | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const ( | ||
emptyToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c" | ||
token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJhenAiOiJjbGllbnRUZXN0In0.eAUlRLw2R2LEvI9TdaD9P6zGQyz-oF7V-Omm2x00iQk" | ||
) | ||
|
||
func TestClientID(t *testing.T) { | ||
|
||
t.Run("empty", func(t *testing.T) { | ||
rec := httptest.NewRecorder() | ||
req := httptest.NewRequest(http.MethodGet, "/", nil) | ||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %v", emptyToken)) | ||
|
||
h := ClientID(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
})) | ||
h.ServeHTTP(rec, req) | ||
assert.Empty(t, rec.Header().Get(ClientIDHeaderName)) | ||
}) | ||
|
||
t.Run("brokenToken", func(t *testing.T) { | ||
rec := httptest.NewRecorder() | ||
req := httptest.NewRequest(http.MethodGet, "/", nil) | ||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %v", emptyToken+"broken")) | ||
|
||
h := ClientID(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
})) | ||
h.ServeHTTP(rec, req) | ||
assert.Empty(t, rec.Header().Get(ClientIDHeaderName)) | ||
}) | ||
|
||
t.Run("clientID", func(t *testing.T) { | ||
rec := httptest.NewRecorder() | ||
req := httptest.NewRequest(http.MethodGet, "/", nil) | ||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %v", token)) | ||
|
||
h := ClientID(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
})) | ||
h.ServeHTTP(rec, req) | ||
assert.NotNil(t, rec.Header().Get(ClientIDHeaderName)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.