Initial commit
This commit is contained in:
commit
2c1f9ce645
2 changed files with 42 additions and 0 deletions
19
jwt.go
Normal file
19
jwt.go
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
package jwt
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
Secret string
|
||||||
|
}
|
||||||
|
|
||||||
|
type JWTMiddleware struct {
|
||||||
|
config Config
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewMiddleware(c *Config) (*JWTMiddleware, error) {
|
||||||
|
if c == nil {
|
||||||
|
return nil, errors.New("missing configuration")
|
||||||
|
}
|
||||||
|
m := &JWTMiddleware{config: *c}
|
||||||
|
return m, nil
|
||||||
|
}
|
23
jwt_test.go
Normal file
23
jwt_test.go
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
package jwt
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestNewJWTMiddleware(t *testing.T) {
|
||||||
|
config := &Config{
|
||||||
|
Secret: "password",
|
||||||
|
}
|
||||||
|
middleware, err := NewMiddleware(config)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("new middleware: %v", err)
|
||||||
|
}
|
||||||
|
if middleware.config.Secret != "password" {
|
||||||
|
t.Errorf("expected 'password', got %v", middleware.config.Secret)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNewJWTMiddlewareNoConfig(t *testing.T) {
|
||||||
|
_, err := NewMiddleware(nil)
|
||||||
|
if err == nil {
|
||||||
|
t.Error("expected configuration error, received none")
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue