Files
gitea/services/oauth2_provider/jwtsigningkey_test.go
T
Max P. a4908d4af4 enhance(actions): add OIDC workload identity support (#39052)
Adds OpenID Connect workload identity to Gitea Actions: jobs declaring
`id-token: write` can request short-lived tokens to authenticate to
cloud providers and other services without stored secrets. Tokens follow
GitHub's format, so clients like `@actions/core` and existing trust
policies carry over.

- Issuer at `/api/actions/oidc` with discovery and JWKS, signed with a
dedicated RS256 key
- GitHub's claims, including `workflow_ref`, `job_workflow_ref` and the
immutable `repo:owner@id/repo@id:ref:...` subject
- `id-token: write` or `write-all` grant it, default permissions and
fork pull requests never get it, owner and repository maximum
permissions can disable it
- Reusable workflows record the exact ref they were loaded from, and
dispatch rejects refs that only resolve through a same-named tag
- Generated signing keys are written atomically, and generated ES384 and
ES512 OAuth2 keys use their own curves instead of P-256
- `github.ref_type` is `branch` for pull request runs, like on GitHub

Fixes https://github.com/go-gitea/gitea/issues/26383
Fixes https://github.com/go-gitea/gitea/issues/33681
Docs: https://gitea.com/gitea/docs/pulls/526
Assisted-by: ChatGPT:gpt-5.6-sol, Claude Code:claude-opus-5-5
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: silverwind <me@silverwind.io>
2026-10-18 07:08:55 +00:00

63 lines
1.8 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package oauth2_provider
import (
"crypto/ecdsa"
"encoding/base64"
"math/big"
"path/filepath"
"testing"
"github.com/golang-jwt/jwt/v5"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestECDSASigningKeyToJWK(t *testing.T) {
for _, tc := range []struct {
signingMethod jwt.SigningMethod
expectedAlg string
expectedCrv string
coordLen int
}{
{jwt.SigningMethodES256, "ES256", "P-256", 32},
{jwt.SigningMethodES384, "ES384", "P-384", 48},
{jwt.SigningMethodES512, "ES512", "P-521", 66},
} {
t.Run(tc.expectedCrv, func(t *testing.T) {
key, err := LoadOrCreateAsymmetricKey(filepath.Join(t.TempDir(), "key.pem"), tc.expectedAlg)
require.NoError(t, err)
privKey, ok := key.(*ecdsa.PrivateKey)
require.True(t, ok)
signingKey, err := newECDSASingingKey(tc.signingMethod, privKey)
require.NoError(t, err)
jwk, err := signingKey.ToJWK()
require.NoError(t, err)
assert.Equal(t, "EC", jwk["kty"])
assert.Equal(t, tc.expectedAlg, jwk["alg"])
assert.Equal(t, tc.expectedCrv, jwk["crv"])
assert.NotEmpty(t, jwk["kid"])
// Verify coordinates are the correct fixed length per RFC 7518 / SEC 1
xBytes, err := base64.RawURLEncoding.DecodeString(jwk["x"])
require.NoError(t, err)
assert.Len(t, xBytes, tc.coordLen)
yBytes, err := base64.RawURLEncoding.DecodeString(jwk["y"])
require.NoError(t, err)
assert.Len(t, yBytes, tc.coordLen)
// Verify the decoded coordinates reconstruct the original public key point
pubKey, ok := privKey.Public().(*ecdsa.PublicKey)
require.True(t, ok)
assert.Equal(t, 0, new(big.Int).SetBytes(xBytes).Cmp(pubKey.X))
assert.Equal(t, 0, new(big.Int).SetBytes(yBytes).Cmp(pubKey.Y))
})
}
}