This repository has been archived by the owner on Jun 26, 2023. It is now read-only.
-
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.
URL parsing is better suited using `net/url`
- Loading branch information
Showing
2 changed files
with
82 additions
and
33 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 |
---|---|---|
|
@@ -25,9 +25,26 @@ func TestUserTokenAuthenticator_NoFile(t *testing.T) { | |
a.pubkeys = make(map[string][]byte) | ||
err := a.getjwtkey("") | ||
assert.Error(t, err) | ||
} | ||
|
||
err = a.getjwtpubkey("") | ||
assert.Error(t, err) | ||
func TestUserTokenAuthenticator_GetFile(t *testing.T) { | ||
// Create temp demo rsa key pair | ||
demoKeysPath := "temp-rsa-keys" | ||
prKeyPath, pubKeyPath, err := helper.MakeFolder(demoKeysPath) | ||
assert.NoError(t, err) | ||
|
||
err = helper.CreateRSAkeys(prKeyPath, pubKeyPath) | ||
assert.NoError(t, err) | ||
|
||
var pubkeys map[string][]byte | ||
jwtpubkeypath := demoKeysPath + "/public-key/" | ||
|
||
a := NewValidateFromToken(pubkeys) | ||
a.pubkeys = make(map[string][]byte) | ||
err = a.getjwtkey(jwtpubkeypath) | ||
assert.NoError(t, err) | ||
|
||
defer os.RemoveAll(demoKeysPath) | ||
} | ||
|
||
func TestUserTokenAuthenticator_WrongURL(t *testing.T) { | ||
|
@@ -37,7 +54,27 @@ func TestUserTokenAuthenticator_WrongURL(t *testing.T) { | |
jwtpubkeyurl := "/dummy/" | ||
|
||
err := a.getjwtpubkey(jwtpubkeyurl) | ||
assert.Error(t, err) | ||
assert.Equal(t, "jwtpubkeyurl is not a proper URL (/dummy/)", err.Error()) | ||
} | ||
|
||
func TestUserTokenAuthenticator_BadURL(t *testing.T) { | ||
var pubkeys map[string][]byte | ||
a := NewValidateFromToken(pubkeys) | ||
a.pubkeys = make(map[string][]byte) | ||
jwtpubkeyurl := "dummy.com/jwk" | ||
|
||
err := a.getjwtpubkey(jwtpubkeyurl) | ||
assert.Equal(t, "parse \"dummy.com/jwk\": invalid URI for request", err.Error()) | ||
} | ||
|
||
func TestUserTokenAuthenticator_GoodURL(t *testing.T) { | ||
var pubkeys map[string][]byte | ||
a := NewValidateFromToken(pubkeys) | ||
a.pubkeys = make(map[string][]byte) | ||
jwtpubkeyurl := "https://example.com/jwk/" | ||
|
||
err := a.getjwtpubkey(jwtpubkeyurl) | ||
assert.ErrorContains(t, err, "failed to fetch remote JWK") | ||
} | ||
|
||
func TestUserTokenAuthenticator_ValidateSignature_RSA(t *testing.T) { | ||
|
@@ -251,6 +288,22 @@ func TestUserTokenAuthenticator_ValidateSignature_EC(t *testing.T) { | |
_, err = a.Authenticate(r) | ||
assert.Contains(t, err.Error(), "failed to get issuer from token") | ||
|
||
// Test LS-AAI user authentication | ||
token, err := helper.CreateECToken(prKeyParsed, "ES256", "JWT", helper.WrongUserClaims) | ||
assert.NoError(t, err) | ||
|
||
r, _ = http.NewRequest("", "/c5773f41d17d27bd53b1e6794aedc32d7906e779_elixir-europe.org/foo", nil) | ||
r.Host = "localhost" | ||
r.Header.Set("X-Amz-Security-Token", token) | ||
_, err = a.Authenticate(r) | ||
assert.NoError(t, err) | ||
|
||
r, _ = http.NewRequest("", "/dataset", nil) | ||
r.Host = "localhost" | ||
r.Header.Set("X-Amz-Security-Token", token) | ||
_, err = a.Authenticate(r) | ||
assert.Equal(t, "token supplied username [email protected] but URL had dataset", err.Error()) | ||
|
||
defer os.RemoveAll(demoKeysPath) | ||
} | ||
|
||
|