Skip to content

Commit

Permalink
Impl constructor for Server, Client and BewitConfig structs.
Browse files Browse the repository at this point in the history
  • Loading branch information
hiyosi committed Oct 27, 2016
1 parent 1f9a960 commit 5dbc97b
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 109 deletions.
44 changes: 18 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ func (c *credentialStore) GetCredential(id string) (*hawk.Credential, error) {
var testCredStore = &credentialStore{}

func hawkHandler(w http.ResponseWriter, r *http.Request) {
s := hawk.Server{
CredentialGetter: testCredStore,
}
s := hawk.NewServer(testCredStore)

// authenticate client request
cred, err := s.Authenticate(r)
Expand Down Expand Up @@ -86,18 +84,18 @@ import (
)

func main() {
c := &hawk.Client{
Credential: &hawk.Credential{
c := hqwk.NewClient(
&hawk.Credential{
ID: "123456",
Key: "werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn",
Alg: hawk.SHA256,
},
Option: &hawk.Option{
&hawk.Option{
TimeStamp: time.Now().Unix(),
Nonce: "3hOHpR",
Ext: "some-app-data",
},
}
)

// build request header
header, _ := c.Header("GET", "http://localhost:8080/resource")
Expand Down Expand Up @@ -133,15 +131,16 @@ func main() {
```.go
// server

b := &hawk.BewitConfig{
Credential: &hawk.Credential{
b := NewBewitConfig(
&hawk.Credential{
ID: "123456",
Key: "werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn",
Alg: hawk.SHA256,
},
Ttl: 10 * time.Minute,
Ext: "some-app-data",
}
10 * time.Minute,
)


bewit := b.GetBewit("http://localhost:8080/temp/resource", nil)
fmt.Println(bewit)

Expand All @@ -153,10 +152,7 @@ func main() {
// server

func hawkBewitHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println("bewit")
s := hawk.Server{
CredentialGetter: testCredStore,
}
s := hawk.NewServer(testCredStore)

cred, err := s.AuthenticateBewit(r)
if err != nil {
Expand All @@ -177,22 +173,18 @@ func hawkBewitHandler(w http.ResponseWriter, r *http.Request) {
- get host-name by specified header name.

```.go
s := hawk.Server{
CredentialGetter: testCredStore,
AuthOption: &hawk.AuthOption{
CustomHostNameHeader: "X-Forwarded-Host",
},
s := NewServer(testCredStore)
s.AuthOption = &hawk.AuthOption{
CustomHostNameHeader: "X-Forwarded-Host",
}
```

- or specified hostname value yourself

```
s := hawk.Server{
CredentialGetter: testCredStore,
AuthOption: &hawk.AuthOption{
CustomHostPort: "b.example.com:8888",
},
s := hawk.NewServer(testCredStore)
s.AuthOption = &hawk.AuthOption{
CustomHostPort: "b.example.com:8888",
}
```

Expand Down
7 changes: 7 additions & 0 deletions bewit.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ type BewitConfig struct {
LocalTimeOffset time.Duration
}

func NewBewitConfig(c *Credential, ttl time.Duration) *BewitConfig {
return &BewitConfig{
Credential: c,
Ttl: ttl,
}
}

// TODO: Implement the SNTP for time sync management

// GetBewit builds a value of bewit parameter.
Expand Down
14 changes: 4 additions & 10 deletions bewit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@ func TestBewitConfig_GetBewit(t *testing.T) {
Alg: SHA256,
}

b1 := &BewitConfig{
Credential: c,
Ttl: 24 * time.Hour * 365 * 100,
Ext: "some-app-data",
}
b1 := NewBewitConfig(c, (24 * time.Hour * 365 * 100))
b1.Ext = "some-app-data"

actual1 := b1.GetBewit("http://example.com/resource/4?a=1&b=2", &stubbedClock{})
expect1 := "MTIzNDU2XDQ1MTkzMTE0NThcYkkwanFlS1prUHE0V1hRMmkxK0NrQ2lOanZEc3BSVkNGajlmbElqMXphWT1cc29tZS1hcHAtZGF0YQ"
Expand All @@ -39,11 +36,8 @@ func TestBewitConfig_GetBewit2(t *testing.T) {
Alg: SHA256,
}

b1 := &BewitConfig{
Credential: c,
Ttl: 24 * time.Hour * 365 * 100,
Ext: "some-app-data",
}
b1 := NewBewitConfig(c, (24 * time.Hour * 365 * 100))
b1.Ext = "some-app-data"

// url parameter is null-string
actual2 := b1.GetBewit("", &stubbedClock{})
Expand Down
7 changes: 7 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ type Client struct {
Option *Option
}

func NewClient(c *Credential, o *Option) *Client {
return &Client{
Credential: c,
Option: o,
}
}

// Header builds a value to be set in the Authorization header.
func (c *Client) Header(method, uri string) (string, error) {
if c.Option.Hash == "" && c.Option.Payload != "" && c.Option.ContentType != "" {
Expand Down
56 changes: 28 additions & 28 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ import (
)

func TestClient_Header(t *testing.T) {
c1 := &Client{
Credential: &Credential{
c1 := NewClient(
&Credential{
ID: "test-id",
Key: "test-key",
Alg: SHA256,
},
Option: &Option{
&Option{
TimeStamp: time.Now().Unix(),
Nonce: "xyz123",
Ext: "sample-ext-string",
},
}
)

url1 := "https://example.com/test/hawk"
act1, err := c1.Header("GET", url1)
Expand All @@ -48,20 +48,20 @@ func TestClient_Header(t *testing.T) {
}

// specified payload
c2 := &Client{
Credential: &Credential{
c2 := NewClient(
&Credential{
ID: "test-id",
Key: "test-key",
Alg: SHA256,
},
Option: &Option{
&Option{
TimeStamp: time.Now().Unix(),
Nonce: "xyz123",
Ext: "sample-ext-string",
ContentType: "text/plain",
Payload: "something to write about",
},
}
)

url2 := "http://example.net/somewhere/over/the/rainbow"
act2, err := c2.Header("POST", url2)
Expand Down Expand Up @@ -89,13 +89,13 @@ func TestClient_Header(t *testing.T) {
}

// specified app and dlg param
c3 := &Client{
Credential: &Credential{
c3 := NewClient(
&Credential{
ID: "test-id",
Key: "test-key",
Alg: SHA256,
},
Option: &Option{
&Option{
TimeStamp: time.Now().Unix(),
Nonce: "xyz123",
Ext: "sample-ext-string",
Expand All @@ -104,7 +104,7 @@ func TestClient_Header(t *testing.T) {
App: "some-app-id",
Dlg: "some-dlg",
},
}
)

url3 := "http://example.net/somewhere/over/the/rainbow"
act3, err := c3.Header("POST", url3)
Expand Down Expand Up @@ -157,18 +157,18 @@ func TestClient_Authenticate(t *testing.T) {
r, _ := http.Get(s.URL)
r.Request.URL = mockedURL

c := &Client{
Credential: &Credential{
c := NewClient(
&Credential{
ID: "123456",
Key: "werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn",
Alg: SHA256,
},
Option: &Option{
&Option{
TimeStamp: ts,
Nonce: "3hOHpR",
Ext: "some-app-data",
},
}
)

act, _ := c.Authenticate(r)
if act != true {
Expand All @@ -186,20 +186,20 @@ func TestClient_Authenticate(t *testing.T) {
r1, _ := http.PostForm(s1.URL, nil)
r1.Request.URL = mockedURL

c1 := &Client{
Credential: &Credential{
c1 := NewClient(
&Credential{
ID: "123456",
Key: "werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn",
Alg: SHA256,
},
Option: &Option{
&Option{
TimeStamp: ts,
Nonce: "3hOHpR",
Ext: "some-app-data",
ContentType: "text/plain",
Payload: "some reply",
},
}
)

act1, _ := c1.Authenticate(r1)
if act1 != true {
Expand Down Expand Up @@ -228,20 +228,20 @@ func TestClient_Authenticate_Fail(t *testing.T) {
r2, _ := http.PostForm(s2.URL, nil)
r2.Request.URL = mockedURL

c2 := &Client{
Credential: &Credential{
c2 := NewClient(
&Credential{
ID: "123456",
Key: "some-key",
Alg: SHA256,
},
Option: &Option{
&Option{
TimeStamp: ts,
Nonce: "3hOHpR",
Ext: "some-app-data",
ContentType: "text/plain",
Payload: "some reply",
},
}
)

act2, _ := c2.Authenticate(r2)
if act2 != false {
Expand All @@ -259,20 +259,20 @@ func TestClient_Authenticate_Fail(t *testing.T) {
r3, _ := http.PostForm(s3.URL, nil)
r3.Request.URL = mockedURL

c3 := &Client{
Credential: &Credential{
c3 := NewClient(
&Credential{
ID: "123456",
Key: "werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn",
Alg: SHA256,
},
Option: &Option{
&Option{
TimeStamp: ts,
Nonce: "3hOHpR",
Ext: "some-app-data",
ContentType: "text/plain",
Payload: "invalid some reply",
},
}
)

act3, _ := c3.Authenticate(r3)
if act3 != false {
Expand Down
15 changes: 11 additions & 4 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

type Server struct {
CredentialGetter CredentialGetter
CredentialStore CredentialStore
NonceValidator NonceValidator
TimeStampSkew time.Duration
LocaltimeOffset time.Duration
Expand All @@ -26,14 +26,21 @@ type AuthOption struct {
CustomClock Clock
}

type CredentialGetter interface {
type CredentialStore interface {
GetCredential(id string) (*Credential, error)
}

type NonceValidator interface {
Validate(key, nonce string, ts int64) bool
}

// NewServer initializies a new Server.
func NewServer(cs CredentialStore) *Server {
return &Server{
CredentialStore: cs,
}
}

// Authenticate authenticate the Hawk request from the HTTP request.
// Successful case returns credential information about requested user.
func (s *Server) Authenticate(req *http.Request) (*Credential, error) {
Expand Down Expand Up @@ -69,7 +76,7 @@ func (s *Server) Authenticate(req *http.Request) (*Credential, error) {
Dlg: authzAttributes["dlg"],
}

cred, err := s.CredentialGetter.GetCredential(authzAttributes["id"])
cred, err := s.CredentialStore.GetCredential(authzAttributes["id"])
if err != nil {
// FIXME: logging error
return nil, errors.New("Failed to get Credential.")
Expand Down Expand Up @@ -185,7 +192,7 @@ func (s *Server) AuthenticateBewit(req *http.Request) (*Credential, error) {
return nil, errors.New("Access expired.")
}

cred, err := s.CredentialGetter.GetCredential(bewit["id"])
cred, err := s.CredentialStore.GetCredential(bewit["id"])
if err != nil {
// FIXME: logging error
return nil, errors.New("Failed to get Credential.")
Expand Down
Loading

0 comments on commit 5dbc97b

Please sign in to comment.