| 1 | // Copyright 2023 The Sigstore Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package emailscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity/email`/ |
| 16 | |
| 17 | import ( |
| 18 | "contextscip-go gomod github.com/golang/go/src go1.25.7 context/" |
| 19 | "encoding/jsonscip-go gomod github.com/golang/go/src go1.25.7 `encoding/json`/" |
| 20 | "testingscip-go gomod github.com/golang/go/src go1.25.7 testing/" |
| 21 | |
| 22 | "github.com/coreos/go-oidc/v3/oidcscip-go gomod github.com/coreos/go-oidc/v3 v3.18.0 `github.com/coreos/go-oidc/v3/oidc`/" |
| 23 | "github.com/sigstore/fulcio/pkg/configscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/config`/" |
| 24 | "github.com/sigstore/fulcio/pkg/identityscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity`/" |
| 25 | ) |
| 26 | |
| 27 | func TestIssuerscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity/email`/TestIssuer().(t *testingscip-go gomod github.com/golang/go/src go1.25.7 testing/.Tscip-go gomod github.com/golang/go/src go1.25.7 testing/T#) { |
| 28 | ctx := contextscip-go gomod github.com/golang/go/src go1.25.7 context/.Backgroundscip-go gomod github.com/golang/go/src go1.25.7 context/Background().() |
| 29 | url := "test-issuer-url" |
| 30 | issuer := Issuerscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity/email`/Issuer().(url) |
| 31 | |
| 32 | // test the Match function |
| 33 | t.Runscip-go gomod github.com/golang/go/src go1.25.7 testing/T#Run().("match", func(t *testingscip-go gomod github.com/golang/go/src go1.25.7 testing/.Tscip-go gomod github.com/golang/go/src go1.25.7 testing/T#) { |
| 34 | if matches := issuer.Matchscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity`/Issuer#Match.(ctx, url); !matches { |
| 35 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.7 testing/common#Fatal().("expected url to match but it doesn't") |
| 36 | } |
| 37 | if matches := issuer.Matchscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity`/Issuer#Match.(ctx, "some-other-url"); matches { |
| 38 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.7 testing/common#Fatal().("expected match to fail but it didn't") |
| 39 | } |
| 40 | }) |
| 41 | |
| 42 | t.Runscip-go gomod github.com/golang/go/src go1.25.7 testing/T#Run().("authenticate", func(t *testingscip-go gomod github.com/golang/go/src go1.25.7 testing/.Tscip-go gomod github.com/golang/go/src go1.25.7 testing/T#) { |
| 43 | token := &oidcscip-go gomod github.com/coreos/go-oidc/v3 v3.18.0 `github.com/coreos/go-oidc/v3/oidc`/.IDTokenscip-go gomod github.com/coreos/go-oidc/v3 v3.18.0 `github.com/coreos/go-oidc/v3/oidc`/IDToken#{ |
| 44 | Issuerscip-go gomod github.com/coreos/go-oidc/v3 v3.18.0 `github.com/coreos/go-oidc/v3/oidc`/IDToken#Issuer.: "https://iss.example.com", |
| 45 | Subjectscip-go gomod github.com/coreos/go-oidc/v3 v3.18.0 `github.com/coreos/go-oidc/v3/oidc`/IDToken#Subject.: "subject", |
| 46 | } |
| 47 | claims, err := jsonscip-go gomod github.com/golang/go/src go1.25.7 `encoding/json`/.Marshalscip-go gomod github.com/golang/go/src go1.25.7 `encoding/json`/Marshal().(map[string]any{ |
| 48 | "aud": "sigstore", |
| 49 | "iss": "https://iss.example.com", |
| 50 | "sub": "doesntmatter", |
| 51 | "email": "[email protected]", |
| 52 | "email_verified": true, |
| 53 | }) |
| 54 | if err != nil { |
| 55 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.7 testing/common#Fatal().(err) |
| 56 | } |
| 57 | withClaimsscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity/email`/withClaims().(token, claims) |
| 58 | |
| 59 | ctx := configscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/config`/.Withscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/config`/With().(contextscip-go gomod github.com/golang/go/src go1.25.7 context/.Backgroundscip-go gomod github.com/golang/go/src go1.25.7 context/Background().(), &configscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/config`/.FulcioConfigscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/config`/FulcioConfig#{ |
| 60 | OIDCIssuersscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/config`/FulcioConfig#OIDCIssuers.: map[string]configscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/config`/.OIDCIssuerscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/config`/OIDCIssuer#{ |
| 61 | "https://iss.example.com": { |
| 62 | IssuerURLscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/config`/OIDCIssuer#IssuerURL.: "https://iss.example.com", |
| 63 | Typescip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/config`/OIDCIssuer#Type.: configscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/config`/.IssuerTypeEmailscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/config`/IssuerTypeEmail., |
| 64 | ClientIDscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/config`/OIDCIssuer#ClientID.: "sigstore", |
| 65 | }, |
| 66 | }, |
| 67 | }) |
| 68 | |
| 69 | identityscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity`/.Authorizescip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity`/Authorize. = func(_ contextscip-go gomod github.com/golang/go/src go1.25.7 context/.Contextscip-go gomod github.com/golang/go/src go1.25.7 context/Context#, _ string, _ ...configscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/config`/.InsecureOIDCConfigOptionscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/config`/InsecureOIDCConfigOption#) (*oidcscip-go gomod github.com/coreos/go-oidc/v3 v3.18.0 `github.com/coreos/go-oidc/v3/oidc`/.IDTokenscip-go gomod github.com/coreos/go-oidc/v3 v3.18.0 `github.com/coreos/go-oidc/v3/oidc`/IDToken#, error) { |
| 70 | return token, nil |
| 71 | } |
| 72 | principal, err := issuer.Authenticatescip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity`/Issuer#Authenticate.(ctx, "token") |
| 73 | if err != nil { |
| 74 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.7 testing/common#Fatal().(err) |
| 75 | } |
| 76 | |
| 77 | if principal.Namescip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity`/Principal#Name.(ctx) != "[email protected]" { |
| 78 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.7 testing/common#Fatalf().("got unexpected name %s", principal.Namescip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity`/Principal#Name.(ctx)) |
| 79 | } |
| 80 | }) |
| 81 | } |