simplify token issuer/verifier interface
This commit is contained in:
+3
-5
@@ -58,9 +58,7 @@ func (m *APIKeyAuthMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request,
|
||||
return
|
||||
}
|
||||
|
||||
v.SetSecretKey(secret)
|
||||
|
||||
grants, err := v.Verify()
|
||||
grants, err := v.Verify(secret)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
w.Write([]byte("invalid token: " + err.Error()))
|
||||
@@ -75,8 +73,8 @@ func (m *APIKeyAuthMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request,
|
||||
next.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
func GetGrants(ctx context.Context) *auth.GrantClaims {
|
||||
claims, ok := ctx.Value(grantsKey).(*auth.GrantClaims)
|
||||
func GetGrants(ctx context.Context) *auth.VideoGrant {
|
||||
claims, ok := ctx.Value(grantsKey).(*auth.VideoGrant)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
@@ -20,16 +19,17 @@ func TestAuthMiddleware(t *testing.T) {
|
||||
provider.GetSecretReturns(secret)
|
||||
|
||||
m := service.NewAPIKeyAuthMiddleware(provider)
|
||||
var grants *auth.GrantClaims
|
||||
var grants *auth.VideoGrant
|
||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
grants = service.GetGrants(r.Context())
|
||||
w.WriteHeader(http.StatusOK)
|
||||
})
|
||||
|
||||
orig := &auth.VideoGrant{Room: "abcdefg", RoomJoin: true}
|
||||
// ensure that the original claim could be retrieved
|
||||
issuer := auth.NewAPIKeyTokenIssuer(api, secret)
|
||||
orig := &auth.GrantClaims{Room: "abcdefg", RoomJoin: true}
|
||||
token, err := issuer.CreateToken(orig, time.Minute)
|
||||
at := auth.NewAccessToken(api, secret).
|
||||
AddGrant(orig)
|
||||
token, err := at.ToJWT()
|
||||
assert.NoError(t, err)
|
||||
|
||||
r := &http.Request{Header: http.Header{}}
|
||||
|
||||
Reference in New Issue
Block a user