-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add UncheckedValidator and use it temporarily
- Loading branch information
Showing
3 changed files
with
91 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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,52 @@ | ||
package token | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/go-jose/go-jose/v4/jwt" | ||
Check failure on line 7 in api/utils/token/unchecked_principal.go GitHub Actions / Unit Test
|
||
) | ||
|
||
func (c *unchechedClaimsPrincipal) Validate(_ context.Context) error { | ||
return nil | ||
} | ||
|
||
type unchechedClaimsPrincipal struct { | ||
token string | ||
claims *jwt.Claims | ||
azureClaims *azureClaims | ||
} | ||
|
||
func (p *unchechedClaimsPrincipal) Token() string { | ||
return p.token | ||
} | ||
func (p *unchechedClaimsPrincipal) IsAuthenticated() bool { | ||
return true | ||
} | ||
func (p *unchechedClaimsPrincipal) Id() string { | ||
if p.azureClaims.ObjectId != "" { | ||
return fmt.Sprintf("unverified: %s", p.azureClaims.ObjectId) | ||
} | ||
|
||
return fmt.Sprintf("unverified: %s (sub!)", p.claims.Subject) | ||
} | ||
|
||
func (p *unchechedClaimsPrincipal) Name() string { | ||
if p.azureClaims.Upn != "" { | ||
return fmt.Sprintf("unverified: %s", p.azureClaims.Upn) | ||
} | ||
|
||
if p.azureClaims.AppDisplayName != "" { | ||
return fmt.Sprintf("unverified: %s", p.azureClaims.AppDisplayName) | ||
} | ||
|
||
if p.azureClaims.AppId != "" { | ||
return fmt.Sprintf("unverified: %s", p.azureClaims.AppId) | ||
} | ||
|
||
if p.azureClaims.ObjectId != "" { | ||
return fmt.Sprintf("unverified: %s", p.azureClaims.ObjectId) | ||
} | ||
|
||
return fmt.Sprintf("unverified: %s", p.claims.Subject) | ||
} |
This file contains 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,36 @@ | ||
package token | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/url" | ||
|
||
"github.com/equinor/radix-common/net/http" | ||
"github.com/go-jose/go-jose/v4" | ||
Check failure on line 9 in api/utils/token/unchecked_validator.go GitHub Actions / Unit Test
|
||
"github.com/go-jose/go-jose/v4/jwt" | ||
) | ||
|
||
type UncheckedValidator struct{} | ||
|
||
var _ ValidatorInterface = &UncheckedValidator{} | ||
|
||
func NewUncheckedValidator(_ *url.URL, _ string) (*UncheckedValidator, error) { | ||
return &UncheckedValidator{}, nil | ||
} | ||
|
||
func (v *UncheckedValidator) ValidateToken(_ context.Context, token string) (TokenPrincipal, error) { | ||
var registeredClaims jwt.Claims | ||
var azureClaims azureClaims | ||
|
||
jwt, err := jwt.ParseSigned(token, []jose.SignatureAlgorithm{jose.HS256, jose.RS256}) | ||
if err != nil { | ||
return nil, http.ForbiddenError("invalid token") | ||
} | ||
err = jwt.UnsafeClaimsWithoutVerification(®isteredClaims, &azureClaims) | ||
if err != nil { | ||
return nil, http.ForbiddenError(fmt.Sprintf("failed to extract JWT unsafeClaims: %w", err)) | ||
} | ||
|
||
principal := &unchechedClaimsPrincipal{token: token, claims: ®isteredClaims, azureClaims: &azureClaims} | ||
return principal, nil | ||
} |
This file contains 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