-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Signed-off-by: Sotirios Mantziaris <[email protected]>
- Loading branch information
Showing
15 changed files
with
371 additions
and
41 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
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,45 @@ | ||
package apikey | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
// Validator interface for validating keys. | ||
type Validator interface { | ||
Validate(key string) (bool, error) | ||
} | ||
|
||
// Authenticator authenticates the request based on the header on the following header key and value: | ||
// Authorization: Apikey {api key}, where {api key} is the key. | ||
type Authenticator struct { | ||
val Validator | ||
} | ||
|
||
// New constructor. | ||
func New(val Validator) (*Authenticator, error) { | ||
if val == nil { | ||
return nil, errors.New("validator is nil") | ||
} | ||
return &Authenticator{val: val}, nil | ||
} | ||
|
||
// Authenticate parses the header for the specified key and authenticates it. | ||
func (a *Authenticator) Authenticate(req *http.Request) (bool, error) { | ||
headerVal := req.Header.Get("Authorization") | ||
if headerVal == "" { | ||
return false, nil | ||
} | ||
|
||
auth := strings.SplitN(headerVal, " ", 2) | ||
if len(auth) != 2 { | ||
return false, nil | ||
} | ||
|
||
if strings.ToLower(auth[0]) != "apikey" { | ||
return false, nil | ||
} | ||
|
||
return a.val.Validate(auth[1]) | ||
} |
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,97 @@ | ||
package apikey | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type MockValidator struct { | ||
err error | ||
success bool | ||
} | ||
|
||
func (mv MockValidator) Validate(key string) (bool, error) { | ||
if mv.err != nil { | ||
return false, mv.err | ||
} | ||
return mv.success, nil | ||
} | ||
|
||
func TestNew(t *testing.T) { | ||
type args struct { | ||
val Validator | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantErr bool | ||
}{ | ||
{name: "success", args: args{val: &MockValidator{}}, wantErr: false}, | ||
{name: "failed due to nil validator", args: args{val: nil}, wantErr: true}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := New(tt.args.val) | ||
if tt.wantErr { | ||
assert.Error(t, err) | ||
assert.Nil(t, got) | ||
} else { | ||
assert.NoError(t, err) | ||
assert.NotNil(t, got) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestAuthenticator_Authenticate(t *testing.T) { | ||
reqOk, err := http.NewRequest("POST", "/test", nil) | ||
assert.NoError(t, err) | ||
reqOk.Header.Set("Authorization", "Apikey 123456") | ||
reqMissingHeader, err := http.NewRequest("POST", "/test", nil) | ||
assert.NoError(t, err) | ||
reqMissingKey, err := http.NewRequest("POST", "/test", nil) | ||
assert.NoError(t, err) | ||
reqMissingKey.Header.Set("Authorization", "Apikey") | ||
reqInvalidAuthMethod, err := http.NewRequest("POST", "/test", nil) | ||
assert.NoError(t, err) | ||
reqInvalidAuthMethod.Header.Set("Authorization", "Bearer 123456") | ||
|
||
type fields struct { | ||
val Validator | ||
} | ||
type args struct { | ||
req *http.Request | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
want bool | ||
wantErr bool | ||
}{ | ||
{name: "authenticated", fields: fields{val: &MockValidator{success: true}}, args: args{req: reqOk}, want: true, wantErr: false}, | ||
{name: "not authenticated, validation failed", fields: fields{val: &MockValidator{success: false}}, args: args{req: reqOk}, want: false, wantErr: false}, | ||
{name: "failed, validation returned err", fields: fields{val: &MockValidator{err: errors.New("TEST")}}, args: args{req: reqOk}, want: false, wantErr: true}, | ||
{name: "not authenticated, header missing", fields: fields{val: &MockValidator{success: false}}, args: args{req: reqMissingHeader}, want: false, wantErr: false}, | ||
{name: "not authenticated, missing key", fields: fields{val: &MockValidator{success: false}}, args: args{req: reqMissingKey}, want: false, wantErr: false}, | ||
{name: "not authenticated, invalid auth method", fields: fields{val: &MockValidator{success: false}}, args: args{req: reqInvalidAuthMethod}, want: false, wantErr: false}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
a := &Authenticator{ | ||
val: tt.fields.val, | ||
} | ||
got, err := a.Authenticate(tt.args.req) | ||
if tt.wantErr { | ||
assert.Error(t, err) | ||
assert.False(t, got) | ||
} else { | ||
assert.NoError(t, err) | ||
assert.Equal(t, tt.want, got) | ||
} | ||
}) | ||
} | ||
} |
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,10 @@ | ||
package auth | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
// Authenticator interface. | ||
type Authenticator interface { | ||
Authenticate(req *http.Request) (bool, error) | ||
} |
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
Oops, something went wrong.