-
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.
NChitty feature/authorization request editor fn (#13)
* Add request editor for auth token * Add basic auth request editor & polish * Return closure instead to allow for user defined value * adjust upstream work --------- Co-authored-by: Nicholas Chitty <[email protected]>
- Loading branch information
1 parent
1d10f30
commit 694c3e5
Showing
5 changed files
with
169 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
// NewBearerAuthorizationRequestFunc returns a RequestEditorFn that adds the authorization bearer token | ||
// to the request | ||
func NewBearerAuthorizationRequestFunc(token string) RequestEditorFn { | ||
return func(ctx context.Context, req *http.Request) error { | ||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token)) | ||
return nil | ||
} | ||
} | ||
|
||
// NewBasicAuthorizationRequestEditor returns a RequestEditorFn that adds the basic authorization headers | ||
// to the request | ||
func NewBasicAuthorizationRequestFunc(username string, password string) RequestEditorFn { | ||
return func(ctx context.Context, req *http.Request) error { | ||
encodedAuth := base64.StdEncoding.EncodeToString([]byte(username + ":" + password)) | ||
req.Header.Add("Authorization", "Basic "+encodedAuth) | ||
return nil | ||
} | ||
} |
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,46 @@ | ||
package client_test | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/promiseofcake/artifactsmmo-go-client/client" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_NewBearerAuthorizationRequestFunc(t *testing.T) { | ||
token := "mock_token_value" | ||
|
||
req := &http.Request{ | ||
Header: make(http.Header), | ||
} | ||
|
||
requestEditor := client.NewBearerAuthorizationRequestFunc(token) | ||
err := requestEditor(context.Background(), req) | ||
require.NoError(t, err) | ||
|
||
expectedHeader := fmt.Sprintf("Bearer %s", token) | ||
authHeader := req.Header.Get("Authorization") | ||
require.Equal(t, expectedHeader, authHeader) | ||
} | ||
|
||
func Test_NewBasicAuthorizationRequestFunc(t *testing.T) { | ||
username := "mock_username_value" | ||
password := "mock_password_value" | ||
|
||
req := &http.Request{ | ||
Header: make(http.Header), | ||
} | ||
|
||
requestEditor := client.NewBasicAuthorizationRequestFunc(username, password) | ||
err := requestEditor(context.Background(), req) | ||
require.NoError(t, err) | ||
|
||
base64Encode := base64.StdEncoding.EncodeToString([]byte(username + ":" + password)) | ||
expectedHeader := fmt.Sprintf("Basic %s", base64Encode) | ||
authHeader := req.Header.Get("Authorization") | ||
require.Equal(t, expectedHeader, authHeader) | ||
} |
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,31 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
const ( | ||
TokenUnsetError = "ARTIFACTSMMO_TOKEN is unset" | ||
UsernameUnsetError = "ARTIFACTSMMO_USERNAME is unset" | ||
PasswordUnsetError = "ARTIFACTSMMO_PASSWORD is unset" | ||
) | ||
|
||
// Returns a request editor that adds the authorization bearer token and does not return an error. | ||
func NewAuthorizationBearerRequestEditor(token string) RequestEditorFn { | ||
return func(ctx context.Context, req *http.Request) error { | ||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token)) | ||
return nil | ||
} | ||
} | ||
|
||
// Returns a request editor that adds the basic authorization header and does not return an error. | ||
func NewBasicAuthorizationRequestEditor(username string, password string, joiner rune, encoding *base64.Encoding) RequestEditorFn { | ||
return func(ctx context.Context, req *http.Request) error { | ||
base64Encode := encoding.EncodeToString([]byte(username + string(joiner) + password)) | ||
req.Header.Add("Authorization", fmt.Sprintf("Basic %s", base64Encode)) | ||
return nil | ||
} | ||
} |
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,64 @@ | ||
package client_test | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/promiseofcake/artifactsmmo-go-client/client" | ||
) | ||
|
||
func TestAddAuthorizationTokenRequestEditor(t *testing.T) { | ||
// Given | ||
token := "mock_token_value" | ||
|
||
req := &http.Request{ | ||
Header: make(http.Header), | ||
} | ||
|
||
// When | ||
requestEditor := client.NewAuthorizationBearerRequestEditor(token) | ||
err := requestEditor(context.Background(), req) | ||
|
||
// Then | ||
if err != nil { | ||
t.Fatalf("expected no error, got %v", err) | ||
} | ||
|
||
expectedHeader := fmt.Sprintf("Bearer %s", token) | ||
authHeader := req.Header.Get("Authorization") | ||
if authHeader != expectedHeader { | ||
t.Fatalf("expected Authorization header %q, got %q", expectedHeader, authHeader) | ||
} | ||
} | ||
|
||
|
||
func TestAddBasicAuthorizationRequestEditor(t *testing.T) { | ||
// Given | ||
username := "mock_username_value" | ||
password := "mock_password_value" | ||
join := ':' | ||
|
||
req := &http.Request{ | ||
Header: make(http.Header), | ||
} | ||
|
||
// When | ||
requestEditor := client.NewBasicAuthorizationRequestEditor(username, password, join, base64.StdEncoding) | ||
err := requestEditor(context.Background(), req) | ||
|
||
// Then | ||
if err != nil { | ||
t.Fatalf("expected no error, got %v", err) | ||
} | ||
|
||
|
||
base64Encode := base64.StdEncoding.EncodeToString([]byte(username + string(join) + password)) | ||
expectedHeader := fmt.Sprintf("Basic %s", base64Encode) | ||
authHeader := req.Header.Get("Authorization") | ||
if authHeader != expectedHeader { | ||
t.Fatalf("expected Authorization header %q, got %q", expectedHeader, authHeader) | ||
} | ||
} |
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