11//
2- // Copyright 2024 The Chainloop Authors.
2+ // Copyright 2024-2026 The Chainloop Authors.
33//
44// Licensed under the Apache License, Version 2.0 (the "License");
55// you may not use this file except in compliance with the License.
@@ -18,8 +18,6 @@ package token
1818import (
1919 "testing"
2020
21- "github.com/golang-jwt/jwt/v4"
22-
2321 v1 "github.com/chainloop-dev/chainloop/pkg/attestation/crafter/api/attestation/v1"
2422 "github.com/stretchr/testify/assert"
2523)
@@ -55,6 +53,18 @@ func TestParse(t *testing.T) {
5553 TokenType : v1 .Attestation_Auth_AUTH_TYPE_FEDERATED ,
5654 },
5755 },
56+ {
57+ name : "federated github token" ,
58+ token : "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL3Rva2VuLmFjdGlvbnMuZ2l0aHVidXNlcmNvbnRlbnQuY29tIiwiYXVkIjoiY2hhaW5sb29wIiwicmVwb3NpdG9yeSI6Im1hdGlhc2luc2F1cnJhbGRlL3Byb2plY3QiLCJzdWIiOiJyZXBvOm1hdGlhc2luc2F1cnJhbGRlL3Byb2plY3Q6cmVmOnJlZnMvaGVhZHMvbWFpbiJ9.signature" ,
59+ want : & ParsedToken {
60+ ID : "https://token.actions.githubusercontent.com" ,
61+ TokenType : v1 .Attestation_Auth_AUTH_TYPE_FEDERATED ,
62+ },
63+ },
64+ {
65+ name : "federated token without issuer" ,
66+ token : "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJjaGFpbmxvb3AifQ.signature" ,
67+ },
5868 {
5969 name : "old api token (without orgID)" ,
6070 token : "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJjcC5jaGFpbmxvb3AiLCJhdWQiOlsiYXBpLXRva2VuLWF1dGguY2hhaW5sb29wIl0sImp0aSI6ImQ0ZTBlZTVlLTk3MTMtNDFkMi05ZmVhLTBiZGIxNDAzMzA4MSJ9.IOd3JIHPwfo9ihU20kvRwLIQJcQtTvp-ajlGqlCD4Es" ,
@@ -83,123 +93,3 @@ func TestParse(t *testing.T) {
8393 })
8494 }
8595}
86-
87- func TestIsGitLabFederatedToken (t * testing.T ) {
88- tests := []struct {
89- name string
90- claims jwt.MapClaims
91- want bool
92- }{
93- {
94- name : "empty claims" ,
95- claims : jwt.MapClaims {},
96- want : false ,
97- },
98- {
99- name : "exactly 10 gitlab" ,
100- claims : jwt.MapClaims {
101- "namespace_id" : "4243" ,
102- "namespace_path" : "chainloop" ,
103- "project_id" : "4242" ,
104- "project_path" : "chainloop/project" ,
105- "user_id" : "123" ,
106- "user_login" : "gitlab-ci-token" ,
107- "user_email" : "ci@gitlab.com" ,
108- "pipeline_id" : "101" ,
109- "job_id" : "202" ,
110- "ref" : "main" ,
111- },
112- want : true ,
113- },
114- {
115- name : "9 gitlab claims" ,
116- claims : jwt.MapClaims {
117- "namespace_id" : "4243" ,
118- "namespace_path" : "chainloop" ,
119- "project_id" : "4242" ,
120- "project_path" : "chainloop/project" ,
121- "user_id" : "123" ,
122- "user_login" : "gitlab-ci-token" ,
123- "pipeline_id" : "101" ,
124- "job_id" : "202" ,
125- "ref" : "main" ,
126- },
127- want : false ,
128- },
129- {
130- name : "all gitlab claims" ,
131- claims : jwt.MapClaims {
132- "namespace_id" : "4243" ,
133- "namespace_path" : "chainloop" ,
134- "project_id" : "4242" ,
135- "project_path" : "chainloop/project" ,
136- "user_id" : "123" ,
137- "user_login" : "gitlab-ci-token" ,
138- "user_email" : "ci@gitlab.com" ,
139- "user_access_level" : "developer" ,
140- "pipeline_id" : "101" ,
141- "pipeline_source" : "push" ,
142- "job_id" : "202" ,
143- "ref" : "main" ,
144- "ref_type" : "branch" ,
145- "ref_protected" : true ,
146- "groups_direct" : []string {"group1" },
147- "environment" : "production" ,
148- "environment_protected" : true ,
149- "deployment_tier" : "production" ,
150- "deployment_action" : "deploy" ,
151- "runner_id" : "runner-1" ,
152- "runner_environment" : "production" ,
153- "sha" : "abc123" ,
154- "ci_config_ref_uri" : "https://gitlab.com" ,
155- "ci_config_sha" : "config-abc123" ,
156- "project_visibility" : "public" ,
157- },
158- want : true ,
159- },
160- {
161- name : "10 gitlab claims mixed with non-gitlab claims" ,
162- claims : jwt.MapClaims {
163- "namespace_id" : "4243" ,
164- "namespace_path" : "chainloop" ,
165- "project_id" : "4242" ,
166- "project_path" : "chainloop/project" ,
167- "user_id" : "123" ,
168- "user_login" : "gitlab-ci-token" ,
169- "user_email" : "ci@gitlab.com" ,
170- "pipeline_id" : "101" ,
171- "job_id" : "202" ,
172- "ref" : "main" ,
173- "custom_claim_1" : "value1" ,
174- "custom_claim_2" : "value2" ,
175- "custom_claim_3" : "value3" ,
176- },
177- want : true ,
178- },
179- {
180- name : "9 gitlab claims mixed with non-gitlab claims" ,
181- claims : jwt.MapClaims {
182- "namespace_id" : "4243" ,
183- "namespace_path" : "chainloop" ,
184- "project_id" : "4242" ,
185- "project_path" : "chainloop/project" ,
186- "user_id" : "123" ,
187- "user_login" : "gitlab-ci-token" ,
188- "pipeline_id" : "101" ,
189- "job_id" : "202" ,
190- "ref" : "main" ,
191- "custom_claim_1" : "value1" ,
192- "custom_claim_2" : "value2" ,
193- "custom_claim_3" : "value3" ,
194- },
195- want : false ,
196- },
197- }
198-
199- for _ , tt := range tests {
200- t .Run (tt .name , func (t * testing.T ) {
201- got := isGitLabFederatedToken (tt .claims )
202- assert .Equal (t , tt .want , got )
203- })
204- }
205- }
0 commit comments