Skip to content

Commit

Permalink
Feature: Carry user auth across services
Browse files Browse the repository at this point in the history
  • Loading branch information
suared committed Aug 10, 2020
1 parent b1c70ae commit 28e1166
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
51 changes: 51 additions & 0 deletions comms/comms.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package comms

import (
"context"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"

"github.com/suared/core/security"
)

//Utilizing the test/e2e to start as a copy to allow this to grow as needed for service to service comms use cases
//only moving Get for now with auth, will add others as the need arises if before eventing issetup
//Starting wtih simple user propogation for current need

//test client setup
var httpClient *http.Client

func init() {
//set local http client for validation
httpClient = &http.Client{Timeout: time.Second * 5}
}

//SimpleGet -Returns the body of an http get request with error if appropriate
func SimpleGet(ctx context.Context, uri string) (string, error) {
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
log.Println(fmt.Sprintf("Comms:SimpleGet:Failed reading request to uri: %v, err:%v", uri, err))
return "", err
}

req.Header.Set("Authorization", security.GetAuth(ctx).GetAuthHeader())

resp, err := httpClient.Do(req)
if err != nil {
log.Println(fmt.Sprintf("Comms:SimpleGet:Failed reading response to: %v, err:%v", uri, err))
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
stringbody := string(body)

// Check the status code is what we expect.
if status := resp.StatusCode; status != http.StatusOK {
err = errors.New(string(status) + ": " + stringbody + ": accessing: " + uri)
}

return stringbody, err
}
11 changes: 10 additions & 1 deletion security/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@ func init() {
//Auth - Interface for setting up and retrieving authentication data
type Auth interface {
GetUser() string
GetAuthHeader() string
IsAdmin() bool
}

//BasicAuth - authentication data holder
type BasicAuth struct {
user string
isAdmin bool
//authHeader - for passing user context forward
authHeader string
}

func (t *BasicAuth) String() string {
Expand All @@ -51,6 +54,11 @@ func (t *BasicAuth) IsAdmin() bool {
return t.isAdmin
}

//GetAuthHeader - returns the Authorization Header associated with this request if http request
func (t *BasicAuth) GetAuthHeader() string {
return t.authHeader
}

//GetAuth - returns Auth from the provided context
func GetAuth(ctx context.Context) Auth {
authKey := ctx.Value(authKey)
Expand Down Expand Up @@ -160,7 +168,8 @@ func SetupAuthFromHTTP(r *http.Request) (context.Context, error) {
if err != nil {
return ctx, fmt.Errorf("unable to validate JWT: %v", err)
}
//

basicAuth.authHeader = authHeader

return context.WithValue(ctx, authKey, &basicAuth), nil
}
Expand Down

0 comments on commit 28e1166

Please sign in to comment.