Path: blob/main/components/public-api-server/pkg/auth/session_jwt.go
2500 views
// Copyright (c) 2023 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package auth56import (7"fmt"8"time"910"github.com/gitpod-io/gitpod/public-api-server/pkg/jws"11"github.com/golang-jwt/jwt/v5"12"github.com/google/uuid"13)1415type SessionClaims struct {16jwt.RegisteredClaims17}1819func NewSessionJWT(subject uuid.UUID, issuer string, issuedAt, expiry time.Time) *jwt.Token {20return jwt.NewWithClaims(jwt.SigningMethodRS256, &SessionClaims{21RegisteredClaims: jwt.RegisteredClaims{22Issuer: issuer,23Subject: subject.String(),24IssuedAt: jwt.NewNumericDate(issuedAt),25ExpiresAt: jwt.NewNumericDate(expiry),26},27})28}2930func VerifySessionJWT(token string, verifier jws.Verifier, expectedIssuer string) (*SessionClaims, error) {31parsed, err := verifier.Verify(token, &SessionClaims{}, jwt.WithIssuer(expectedIssuer))32if err != nil {33return nil, fmt.Errorf("failed to parse jwt: %w", err)34}3536claims, ok := parsed.Claims.(*SessionClaims)37if !ok {38return nil, fmt.Errorf("unknown jwt claims: %w", jwt.ErrTokenInvalidClaims)39}4041return claims, nil42}434445