-
Notifications
You must be signed in to change notification settings - Fork 848
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5897 from concourse/issue/5851
Generate opaque access tokens
- Loading branch information
Showing
62 changed files
with
2,153 additions
and
1,339 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
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 |
---|---|---|
@@ -1,24 +1,63 @@ | ||
package accessor | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/concourse/concourse/atc/db" | ||
) | ||
|
||
//go:generate counterfeiter . TokenVerifier | ||
|
||
type TokenVerifier interface { | ||
Verify(req *http.Request) (map[string]interface{}, error) | ||
} | ||
|
||
//go:generate counterfeiter . TeamFetcher | ||
|
||
type TeamFetcher interface { | ||
GetTeams() ([]db.Team, error) | ||
} | ||
|
||
func NewAccessFactory( | ||
tokenVerifier TokenVerifier, | ||
teamFetcher TeamFetcher, | ||
systemClaimKey string, | ||
systemClaimValues []string, | ||
) AccessFactory { | ||
return &accessFactory{ | ||
tokenVerifier: tokenVerifier, | ||
teamFetcher: teamFetcher, | ||
systemClaimKey: systemClaimKey, | ||
systemClaimValues: systemClaimValues, | ||
} | ||
} | ||
|
||
type accessFactory struct { | ||
tokenVerifier TokenVerifier | ||
teamFetcher TeamFetcher | ||
systemClaimKey string | ||
systemClaimValues []string | ||
} | ||
|
||
func (a *accessFactory) Create(role string, verification Verification, teams []db.Team) Access { | ||
return NewAccessor(verification, role, a.systemClaimKey, a.systemClaimValues, teams) | ||
func (a *accessFactory) Create(req *http.Request, role string) (Access, error) { | ||
teams, err := a.teamFetcher.GetTeams() | ||
if err != nil { | ||
return nil, fmt.Errorf("fetch teams: %w", err) | ||
} | ||
return NewAccessor(a.verifyToken(req), role, a.systemClaimKey, a.systemClaimValues, teams), nil | ||
} | ||
|
||
func (a *accessFactory) verifyToken(req *http.Request) Verification { | ||
claims, err := a.tokenVerifier.Verify(req) | ||
if err != nil { | ||
switch err { | ||
case ErrVerificationNoToken: | ||
return Verification{HasToken: false, IsTokenValid: false} | ||
default: | ||
return Verification{HasToken: true, IsTokenValid: false} | ||
} | ||
} | ||
|
||
return Verification{HasToken: true, IsTokenValid: true, RawClaims: claims} | ||
} |
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
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
Oops, something went wrong.