Skip to content
This repository has been archived by the owner on Feb 8, 2023. It is now read-only.

Commit

Permalink
add function to convert string keys
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Apr 17, 2017
1 parent 990586c commit a6531fd
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 7 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -2,6 +2,7 @@ sudo: false
language: go
go:
- 1.8
- 1.8.1
before_install:
- go get -t -v ./...
- go get github.com/modocache/gover
Expand Down
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -13,7 +13,7 @@ https://github.com/teambition/crypto-go

## Demo

### Create a token and verify it.
### Create a token and verify it

```go
auther := auth.New([]byte("key1"))
Expand All @@ -23,7 +23,7 @@ fmt.Println(claims.Get("test"))
// Output: "OK"
```

### Use with Gear.
### Use with Gear

```go
package main
Expand Down
2 changes: 1 addition & 1 deletion auth.go
Expand Up @@ -9,7 +9,7 @@ import (
)

// Version ...
const Version = "1.5.1"
const Version = "1.5.2"

// TokenExtractor is a function that takes a gear.Context as input and
// returns either a string token or an empty string. Default to:
Expand Down
14 changes: 11 additions & 3 deletions jwt/jwt.go
Expand Up @@ -113,23 +113,23 @@ func (j *JWT) SetExpiresIn(expiresIn time.Duration) {
// SetKeys set new keys to jwt.
func (j *JWT) SetKeys(keys ...interface{}) {
if len(keys) == 0 || keys[0] == nil {
panic(errors.New("Invalid keys"))
panic(errors.New("invalid keys"))
}
j.keys = keys
}

// SetMethods set one or more signing methods which can be used rotational.
func (j *JWT) SetMethods(method josecrypto.SigningMethod) {
if method == nil {
panic(errors.New("Invalid signing method"))
panic(errors.New("invalid signing method"))
}
j.method = method
}

// SetValidator set a custom jwt.Validator to jwt. Default to nil.
func (j *JWT) SetValidator(validator *josejwt.Validator) {
if validator == nil {
panic(errors.New("Invalid validator"))
panic(errors.New("invalid validator"))
}
j.validator = []*josejwt.Validator{validator}
}
Expand Down Expand Up @@ -185,3 +185,11 @@ func (r rotating) Verify(v func(interface{}) bool) (index int) {
}
return -1
}

// StrToKeys converts string slice to keys slice.
func StrToKeys(keys ...string) (res []interface{}) {
for _, key := range keys {
res = append(res, []byte(key))
}
return
}
2 changes: 1 addition & 1 deletion jwt/jwt_test.go
Expand Up @@ -29,7 +29,7 @@ func TestJWT(t *testing.T) {

jwter0 := New()
jwter1 := New([]byte("key1"))
jwter2 := New([]byte("key2"))
jwter2 := New(StrToKeys("key2")...)
assert.Equal(1, len(jwter1.keys))
assert.Equal(crypto.SigningMethodHS256, jwter1.method)

Expand Down

0 comments on commit a6531fd

Please sign in to comment.