Skip to content

Commit 229e2a7

Browse files
committed
JWT plugin config parsing
1 parent 890ee07 commit 229e2a7

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

plugins/jwt/jwt.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,37 @@
11
package jwt
22

33
import (
4+
"errors"
5+
"fmt"
6+
"github.com/BurntSushi/toml"
7+
"io/ioutil"
48
"time"
59
)
610

11+
type duration struct {
12+
time.Duration
13+
}
14+
15+
func (d *duration) UnmarshalText(text []byte) error {
16+
var err error
17+
d.Duration, err = time.ParseDuration(string(text))
18+
return err
19+
}
20+
721
type JWT struct {
8-
Secret []byte
22+
Secret string
923
Issuer string
10-
ExpirationTime time.Time
11-
RotationDeadline time.Time
24+
ExpirationTime duration `toml:"expiration"`
25+
RotationDeadline duration `toml:"rotation_deadline"`
1226
}
1327

1428
func (self *JWT) ParseConfig(path string) error {
15-
return nil
29+
content, err := ioutil.ReadFile(path)
30+
if err != nil {
31+
return errors.New(fmt.Sprintf("Error while reading plugin config: %v", err))
32+
}
33+
_, err = toml.Decode(string(content), self)
34+
return err
1635
}
1736

1837
// app.Register(&JWT{}, "jwt") || app.Register("jwt", JWT)

plugins/jwt/jwt_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package jwt
22

33
import (
44
"testing"
5+
"time"
56
)
67

78
func TestParseConfig(t *testing.T) {
@@ -10,7 +11,16 @@ func TestParseConfig(t *testing.T) {
1011
if err != nil {
1112
t.Error(err)
1213
}
13-
if string(jwtPlugin.Secret) != "secret123" {
14-
t.Errorf("Secret key was parsed incorrectly. Expected: '%v', but got: '%v'", "secret123", string(jwtPlugin.Secret))
14+
if jwtPlugin.Secret != "secret123" {
15+
t.Errorf("Secret attribute was parsed incorrectly. Expected: '%v', but got: '%v'", "secret123", jwtPlugin.Secret)
16+
}
17+
if jwtPlugin.Issuer != "issuer" {
18+
t.Errorf("Issuer attribute was parsed incorrectly. Expected: '%v', but got: '%v'", "issuer", jwtPlugin.Issuer)
19+
}
20+
if jwtPlugin.ExpirationTime.Duration != time.Duration(time.Hour*4) {
21+
t.Errorf("Expected 4 hour expiratio time, but got: %v", jwtPlugin.ExpirationTime.Duration)
22+
}
23+
if jwtPlugin.RotationDeadline.Duration != time.Duration(time.Hour*2) {
24+
t.Errorf("Expected 2 hour rotation deadline, but got: %v", jwtPlugin.RotationDeadline.Duration)
1525
}
1626
}

0 commit comments

Comments
 (0)