Skip to content

Commit

Permalink
NChitty feature/authorization request editor fn (#13)
Browse files Browse the repository at this point in the history
* 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
promiseofcake and NChitty authored Aug 3, 2024
1 parent 1d10f30 commit 694c3e5
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 0 deletions.
27 changes: 27 additions & 0 deletions client/helpers.go
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
}
}
46 changes: 46 additions & 0 deletions client/helpers_test.go
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)
}
31 changes: 31 additions & 0 deletions client/request-editors.go
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
}
}
64 changes: 64 additions & 0 deletions client/request-editors_test.go
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)
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.22.5
require (
github.com/oapi-codegen/oapi-codegen/v2 v2.3.0
github.com/oapi-codegen/runtime v1.1.1
github.com/stretchr/testify v1.9.0
)

require (
Expand Down

0 comments on commit 694c3e5

Please sign in to comment.