-
Notifications
You must be signed in to change notification settings - Fork 0
/
signin.go
65 lines (52 loc) · 1.52 KB
/
signin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package clients
import (
signin "clients/gen/signin"
"context"
"log"
"time"
jwt "github.com/dgrijalva/jwt-go"
"goa.design/goa/v3/security"
)
// signin service example implementation.
// The example methods log the requests and return zero values.
type signinsrvc struct {
logger *log.Logger
}
// NewSignin returns the signin service implementation.
func NewSignin(logger *log.Logger) signin.Service {
return &signinsrvc{logger}
}
// BasicAuth implements the authorization logic for service "signin" for the
// "basic" security scheme.
func (s *signinsrvc) BasicAuth(ctx context.Context,
user, pass string, scheme *security.BasicScheme) (context.Context,
error) {
if user != "gopher" && pass != "academy" {
return ctx, signin.
Unauthorized("invalid username and password combination")
}
return ctx, nil
}
// Creates a valid JWT
func (s *signinsrvc) Authenticate(ctx context.Context,
p *signin.AuthenticatePayload) (res *signin.Creds,
err error) {
// create JWT token
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"nbf": time.Date(2015, 10, 10, 12, 0, 0, 0, time.UTC).Unix(),
"iat": time.Now().Unix(),
"exp": time.Now().Add(time.Duration(9) * time.Minute).Unix(),
"scopes": []string{"api:read", "api:write"},
})
s.logger.Printf("user '%s' logged in", p.Username)
// note that if "SignedString" returns an error then it is returned as
// an internal error to the client
t, err := token.SignedString(Key)
if err != nil {
return nil, err
}
res = &signin.Creds{
JWT: t,
}
return
}