Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authentication client #129

Merged
merged 3 commits into from
Dec 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
28 changes: 15 additions & 13 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import:
version: 1.3.0
- package: github.com/prometheus/client_golang
version: 0.8.0
- package: github.com/sony/gobreaker
version: 0.3.0
- package: go.uber.org/zap
version: 1.7.0
- package: golang.org/x/crypto
Expand Down
10 changes: 5 additions & 5 deletions http/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ The service is configured using the environment variables presented in the
following table. Note that any unset variables will be replaced with their
default values.

| Variable | Description | Default |
|------------------|-------------------|-----------------------|
| ADAPTER_NATS_URL | NATS instance URL | nats://localhost:4222 |
| Variable | Description | Default |
|-----------------------|-------------------|-----------------------|
| HTTP_ADAPTER_NATS_URL | NATS instance URL | nats://localhost:4222 |

## Deployment

Expand All @@ -27,7 +27,7 @@ services:
ports:
- [host machine port]:8180
environment:
ADAPTER_NATS_URL: [NATS instance URL]
HTTP_ADAPTER_NATS_URL: [NATS instance URL]
```

To start the service outside of the container, execute the following shell script:
Expand All @@ -42,7 +42,7 @@ cd $GOPATH/src/github.com/mainflux/mainflux/cmd/http
CGO_ENABLED=0 GOOS=[platform identifier] go build -ldflags "-s" -a -installsuffix cgo -o app

# set the environment variables and run the service
ADAPTER_NATS_URL=[NATS instance URL] app
HTTP_ADAPTER_NATS_URL=[NATS instance URL] app
```

## Usage
Expand Down
82 changes: 82 additions & 0 deletions manager/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Package client provides a manager service client intended for internal
// service communication.
package client

import (
"errors"
"net/http"
"time"

"github.com/mainflux/mainflux/manager"
"github.com/sony/gobreaker"
)

const (
timeout = time.Second * 5
maxFailedReqs = 3
maxFailureRatio = 0.6
)

// ErrServiceUnreachable indicates that the service instance is not available.
var ErrServiceUnreachable = errors.New("manager service unavailable")

type managerClient struct {
url string
cb *gobreaker.CircuitBreaker
}

// NewClient instantiates the manager service client given its base URL.
func NewClient(url string) managerClient {
st := gobreaker.Settings{
Name: "Manager",
ReadyToTrip: func(counts gobreaker.Counts) bool {
fr := float64(counts.TotalFailures) / float64(counts.Requests)
return counts.Requests >= maxFailedReqs && fr >= maxFailureRatio
},
}

mc := managerClient{
url: url,
cb: gobreaker.NewCircuitBreaker(st),
}

return mc
}

func (mc managerClient) Authenticate(req *http.Request) (string, error) {
response, err := mc.cb.Execute(func() (interface{}, error) {
hc := &http.Client{
Timeout: timeout,
}

mgReq, err := http.NewRequest("POST", mc.url+"/identity", nil)
if err != nil {
return "", ErrServiceUnreachable
}

mgReq.Header.Set("Authorization", req.Header.Get("Authorization"))

res, err := hc.Do(mgReq)
defer res.Body.Close()

if err != nil {
return "", ErrServiceUnreachable
}

if res.StatusCode != http.StatusOK {
return manager.ErrUnauthorizedAccess, nil
}

return res.Header.Get("X-Client-Id"), nil
})

if err != nil {
return "", err
}

if key, ok := response.(string); !ok {
return "", manager.ErrUnauthorizedAccess
} else {
return key, nil
}
}
9 changes: 3 additions & 6 deletions vendor/github.com/go-stack/stack/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 67 additions & 0 deletions vendor/github.com/go-stack/stack/stack-go19_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading