Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions tavern/internal/auth/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"net/http"
"time"

"github.com/golang-jwt/jwt"
"github.com/golang-jwt/jwt/v5"
"golang.org/x/oauth2"
"realm.pub/tavern/internal/ent"
"realm.pub/tavern/internal/ent/user"
Expand Down Expand Up @@ -79,7 +79,7 @@ func NewOAuthAuthorizationHandler(cfg oauth2.Config, pubKey ed25519.PublicKey, g
}

// Verify JWT in provided OAuth cookie
var claims jwt.StandardClaims
var claims jwt.RegisteredClaims
stateToken, err := jwt.ParseWithClaims(oauthCookie.Value, &claims, func(stateToken *jwt.Token) (interface{}, error) {
// Ensure ed25519 was used
if _, ok := stateToken.Method.(*jwt.SigningMethodEd25519); !ok {
Expand All @@ -94,7 +94,7 @@ func NewOAuthAuthorizationHandler(cfg oauth2.Config, pubKey ed25519.PublicKey, g
}

// Ensure presented OAuth state matches expected (stored in JWT)
if presentedState != claims.Id {
if presentedState != claims.ID {
http.Error(w, ErrOAuthInvalidState.Error(), http.StatusUnauthorized)
return
}
Expand Down Expand Up @@ -187,10 +187,10 @@ func NewOAuthAuthorizationHandler(cfg oauth2.Config, pubKey ed25519.PublicKey, g

func newOAuthStateCookie(privKey ed25519.PrivateKey, state string) *http.Cookie {
expiresAt := time.Now().Add(10 * time.Minute)
claims := jwt.StandardClaims{
Id: state,
IssuedAt: time.Now().Unix(),
ExpiresAt: expiresAt.Unix(),
claims := jwt.RegisteredClaims{
ID: state,
IssuedAt: jwt.NewNumericDate(time.Now()),
ExpiresAt: jwt.NewNumericDate(expiresAt),
}
token := jwt.NewWithClaims(&jwt.SigningMethodEd25519{}, claims)
tokenStr, err := token.SignedString(privKey)
Expand Down
22 changes: 11 additions & 11 deletions tavern/internal/auth/oauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"testing"
"time"

"github.com/golang-jwt/jwt"
"github.com/golang-jwt/jwt/v5"
_ "github.com/mattn/go-sqlite3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -63,7 +63,7 @@ func TestNewOAuthLoginHandler(t *testing.T) {
require.NotNil(t, oauthCookie)

// Parse JWT
var claims jwt.StandardClaims
var claims jwt.RegisteredClaims
token, err := jwt.ParseWithClaims(oauthCookie.Value, &claims, func(token *jwt.Token) (interface{}, error) {
assert.IsType(t, token.Method, &jwt.SigningMethodEd25519{})
return pubKey, nil
Expand All @@ -72,10 +72,10 @@ func TestNewOAuthLoginHandler(t *testing.T) {
assert.True(t, token.Valid)

// JWT assertions
assert.Equal(t, state, claims.Id)
assert.GreaterOrEqual(t, claims.IssuedAt, time.Now().Add(-60*time.Second).Unix())
assert.Greater(t, claims.ExpiresAt, time.Now().Add(9*time.Minute).Unix())
assert.Less(t, claims.ExpiresAt, time.Now().Add(11*time.Minute).Unix())
assert.Equal(t, state, claims.ID)
assert.GreaterOrEqual(t, claims.IssuedAt.Unix(), time.Now().Add(-60*time.Second).Unix())
assert.Greater(t, claims.ExpiresAt.Unix(), time.Now().Add(9*time.Minute).Unix())
assert.Less(t, claims.ExpiresAt.Unix(), time.Now().Add(11*time.Minute).Unix())
}

// TestNewOAuthAuthorizationHandler ensures the OAuth Authorization Handler exhibits expected behaviour
Expand Down Expand Up @@ -192,18 +192,18 @@ func getSessionCookie(cookies []*http.Cookie) *http.Cookie {
func getOAuthAuthorizationRequest(t *testing.T, privKey ed25519.PrivateKey, code string) *http.Request {
// Create a JWT
expiresAt := time.Now().Add(10 * time.Minute)
claims := jwt.StandardClaims{
Id: "ABCDEFG",
IssuedAt: time.Now().Unix(),
ExpiresAt: expiresAt.Unix(),
claims := jwt.RegisteredClaims{
ID: "ABCDEFG",
IssuedAt: jwt.NewNumericDate(time.Now()),
ExpiresAt: jwt.NewNumericDate(expiresAt),
}
token := jwt.NewWithClaims(&jwt.SigningMethodEd25519{}, claims)
tokenStr, err := token.SignedString(privKey)
require.NoError(t, err)

req := httptest.NewRequest(http.MethodGet, "/oauth/authorize", nil)
params := url.Values{
"state": []string{claims.Id},
"state": []string{claims.ID},
"code": []string{code},
}
req.AddCookie(&http.Cookie{
Expand Down
2 changes: 1 addition & 1 deletion tavern/internal/c2/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strings"
"time"

"github.com/golang-jwt/jwt"
"github.com/golang-jwt/jwt/v5"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/peer"
"realm.pub/tavern/internal/c2/c2pb"
Expand Down
2 changes: 1 addition & 1 deletion tavern/internal/c2/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"testing"
"time"

"github.com/golang-jwt/jwt"
"github.com/golang-jwt/jwt/v5"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/peer"
Expand Down
Loading