-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
266 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
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
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,95 @@ | ||
package admin | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
|
||
"connectrpc.com/authn" | ||
"github.com/go-logr/logr" | ||
|
||
"github.com/artefactual/archivematica/hack/ccp/internal/store" | ||
) | ||
|
||
var errInvalidAuth = authn.Errorf("invalid authorization") | ||
|
||
func authenticate(logger logr.Logger, store store.Store) authn.AuthFunc { | ||
return multiAuthenticate( | ||
authApiKey(logger, store), | ||
) | ||
} | ||
|
||
func multiAuthenticate(methods ...authn.AuthFunc) authn.AuthFunc { | ||
return func(ctx context.Context, req authn.Request) (any, error) { | ||
var lastErr error | ||
for _, method := range methods { | ||
result, err := method(ctx, req) | ||
if err == nil { | ||
return result, nil | ||
} | ||
lastErr = err | ||
} | ||
return nil, lastErr | ||
} | ||
} | ||
|
||
func authApiKey(logger logr.Logger, store store.Store) authn.AuthFunc { | ||
return func(ctx context.Context, req authn.Request) (any, error) { | ||
auth := req.Header().Get("Authorization") | ||
if auth == "" { | ||
return nil, errInvalidAuth | ||
} | ||
|
||
username, key, ok := parseApiKey(auth) | ||
if !ok { | ||
return nil, errInvalidAuth | ||
} | ||
|
||
ok, err := store.ValidateUserAPIKey(ctx, username, key) | ||
if err != nil { | ||
logger.Error(err, "Cannot look up user details.") | ||
return nil, errInvalidAuth | ||
} | ||
if !ok { | ||
return nil, errInvalidAuth | ||
} | ||
|
||
return username, nil | ||
} | ||
} | ||
|
||
// parseApiKey parses the ApiKey string. | ||
// "ApiKey test:test" returns ("test", "test", true). | ||
func parseApiKey(auth string) (username, key string, ok bool) { | ||
const prefix = "ApiKey " | ||
// Case insensitive prefix match. | ||
if len(auth) < len(prefix) || !equalFold(auth[:len(prefix)], prefix) { | ||
return "", "", false | ||
} | ||
username, key, ok = strings.Cut(auth[len(prefix):], ":") | ||
if !ok { | ||
return "", "", false | ||
} | ||
return username, key, true | ||
} | ||
|
||
// equalFold is [strings.EqualFold], ASCII only. It reports whether s and t | ||
// are equal, ASCII-case-insensitively. | ||
func equalFold(s, t string) bool { | ||
if len(s) != len(t) { | ||
return false | ||
} | ||
for i := range len(s) { | ||
if lower(s[i]) != lower(t[i]) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
// lower returns the ASCII lowercase version of b. | ||
func lower(b byte) byte { | ||
if 'A' <= b && b <= 'Z' { | ||
return b + ('a' - 'A') | ||
} | ||
return b | ||
} |
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,56 @@ | ||
package admin | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"connectrpc.com/authn" | ||
"github.com/artefactual/archivematica/hack/ccp/internal/store/storemock" | ||
"github.com/go-logr/logr" | ||
"go.artefactual.dev/tools/mockutil" | ||
"go.uber.org/mock/gomock" | ||
"gotest.tools/v3/assert" | ||
) | ||
|
||
func TestAuthentication(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("Accepts API key", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
store := storemock.NewMockStore(gomock.NewController(t)) | ||
store.EXPECT().ValidateUserAPIKey(mockutil.Context(), "test", "test").Return(true, nil) | ||
|
||
auth := multiAuthenticate(authApiKey(logr.Discard(), store)) | ||
handler := authn.NewMiddleware(auth).Wrap(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})) | ||
|
||
req := httptest.NewRequest("GET", "http://example.com/foo", nil) | ||
req.Header.Set("Authorization", "ApiKey test:test") | ||
w := httptest.NewRecorder() | ||
handler.ServeHTTP(w, req) | ||
|
||
resp := w.Result() | ||
|
||
assert.Equal(t, resp.StatusCode, http.StatusOK) | ||
}) | ||
|
||
t.Run("Rejects invalid API key", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
store := storemock.NewMockStore(gomock.NewController(t)) | ||
store.EXPECT().ValidateUserAPIKey(mockutil.Context(), "test", "12345").Return(false, nil) | ||
|
||
auth := multiAuthenticate(authApiKey(logr.Discard(), store)) | ||
handler := authn.NewMiddleware(auth).Wrap(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})) | ||
|
||
req := httptest.NewRequest("GET", "http://example.com/foo", nil) | ||
req.Header.Set("Authorization", "ApiKey test:12345") | ||
w := httptest.NewRecorder() | ||
handler.ServeHTTP(w, req) | ||
|
||
resp := w.Result() | ||
|
||
assert.Equal(t, resp.StatusCode, http.StatusUnauthorized) | ||
}) | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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