-
Notifications
You must be signed in to change notification settings - Fork 38
feat: store authentication identifier in attestation #2139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Piskoo
merged 7 commits into
chainloop-dev:main
from
Piskoo:feat-add-authentication-identifier-to-attestation
Jun 30, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
67a4865
feat(attestation): store authentication identifier in attestation
Piskoo d2dcb7e
fix: lint errors
Piskoo dba70ba
fix: lint errors
Piskoo 624ced3
rename parse token function
Piskoo ec0a482
rename add auth type enum
Piskoo dc26c5e
move token package to cli internal
Piskoo 9936e6a
fix crafting state indentation
Piskoo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// | ||
// Copyright 2024-2025 The Chainloop Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package token | ||
|
||
import ( | ||
v1 "github.com/chainloop-dev/chainloop/pkg/attestation/crafter/api/attestation/v1" | ||
"github.com/golang-jwt/jwt/v4" | ||
) | ||
|
||
const ( | ||
UserAudience = "user-auth.chainloop" | ||
APIAudience = "api-token-auth.chainloop" | ||
) | ||
|
||
type ParsedToken struct { | ||
ID string | ||
OrgID string | ||
TokenType v1.Attestation_Auth_AuthType | ||
} | ||
|
||
const ( | ||
userAudience = "user-auth.chainloop" | ||
//nolint:gosec | ||
apiTokenAudience = "api-token-auth.chainloop" | ||
) | ||
|
||
// Parse the token and return the type of token. At the moment in Chainloop we have 3 types of tokens: | ||
// 1. User account token | ||
// 2. API token | ||
// Each one of them have an associated audience claim that we use to identify the type of token. If the token is not | ||
// present, nor we cannot match it with one of the expected audience, return nil. | ||
func Parse(token string) (*ParsedToken, error) { | ||
if token == "" { | ||
return nil, nil | ||
} | ||
|
||
// Create a parser without claims validation | ||
parser := jwt.NewParser(jwt.WithoutClaimsValidation()) | ||
|
||
// Parse the token without verification | ||
t, _, err := parser.ParseUnverified(token, jwt.MapClaims{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Extract generic claims otherwise, we would have to parse | ||
// the token again to get the claims for each type | ||
claims, ok := t.Claims.(jwt.MapClaims) | ||
if !ok { | ||
return nil, nil | ||
} | ||
|
||
// Get the audience claim | ||
val, ok := claims["aud"] | ||
if !ok || val == nil { | ||
return nil, nil | ||
} | ||
|
||
// Ensure audience is an array of interfaces | ||
// Chainloop only has one audience per token | ||
aud, ok := val.([]interface{}) | ||
if !ok || len(aud) == 0 { | ||
return nil, nil | ||
} | ||
|
||
// Initialize parsedToken | ||
pToken := &ParsedToken{} | ||
|
||
// Determine the type of token based on the audience. | ||
switch aud[0].(string) { | ||
case apiTokenAudience: | ||
pToken.TokenType = v1.Attestation_Auth_AUTH_TYPE_API_TOKEN | ||
if tokenID, ok := claims["jti"].(string); ok { | ||
pToken.ID = tokenID | ||
} | ||
if orgID, ok := claims["org_id"].(string); ok { | ||
pToken.OrgID = orgID | ||
} | ||
case userAudience: | ||
pToken.TokenType = v1.Attestation_Auth_AUTH_TYPE_USER | ||
if userID, ok := claims["user_id"].(string); ok { | ||
pToken.ID = userID | ||
} | ||
default: | ||
return nil, nil | ||
} | ||
|
||
return pToken, nil | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we add the Federated one, we will have 3 types and this sentence will be true 😆