Skip to content

Commit d003466

Browse files
authored
Refactor error messages to include more detail (#37)
1 parent c7d05ac commit d003466

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

pkg/auth/auth.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ func NewAuthorization(cfg *config.Configuration) (Authorization, error) {
7575
return authz, nil
7676
}
7777

78+
// GetEndpoints returns all endpoints.
7879
func (a *Authorization) GetEndpoints() map[string]*Endpoint {
7980
return a.endpoints
8081
}
@@ -91,18 +92,18 @@ func (a *Authorization) LookupEndpoint(domain, org, proj, repo string) (*Endpoin
9192

9293
// PatForToken returns the pat associated with the token.
9394
func (a *Authorization) GetPatForToken(token string) (string, error) {
94-
e, ok := a.endpoints[token]
95-
if !ok {
96-
return "", errors.New("invalid token")
95+
e, err := a.GetEndpointForToken(token)
96+
if err != nil {
97+
return "", err
9798
}
9899
return e.Pat, nil
99100
}
100101

101102
// TargetForToken returns the target url which matches the given token.
102103
func (a *Authorization) GetTargetForToken(token string) (*url.URL, error) {
103-
e, ok := a.endpoints[token]
104-
if !ok {
105-
return nil, errors.New("invalid token")
104+
e, err := a.GetEndpointForToken(token)
105+
if err != nil {
106+
return nil, err
106107
}
107108
target, err := url.Parse(fmt.Sprintf("%s://%s", e.Scheme, e.Domain))
108109
if err != nil {
@@ -113,16 +114,25 @@ func (a *Authorization) GetTargetForToken(token string) (*url.URL, error) {
113114

114115
// IsPermitted checks if a specific token is permitted to access a path.
115116
func (a *Authorization) IsPermitted(path string, token string) error {
116-
e, ok := a.endpoints[token]
117-
if !ok {
118-
return errors.New("invalid token")
117+
e, err := a.GetEndpointForToken(token)
118+
if err != nil {
119+
return err
119120
}
120121
for _, r := range e.regexes {
121122
if r.MatchString(path) {
122123
return nil
123124
}
124125
}
125-
return fmt.Errorf("invalid token")
126+
return fmt.Errorf("token not permitted for path %s", path)
127+
}
128+
129+
// GetEndpointForToken returns an endpoint for the specified token.
130+
func (a *Authorization) GetEndpointForToken(token string) (*Endpoint, error) {
131+
e, ok := a.endpoints[token]
132+
if !ok {
133+
return nil, errors.New("endpoint not found for token")
134+
}
135+
return e, nil
126136
}
127137

128138
func randomSecureToken() (string, error) {

0 commit comments

Comments
 (0)