/
enrollment.go
95 lines (88 loc) · 2.98 KB
/
enrollment.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package enrollment
import (
"context"
"fmt"
"strings"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
"github.com/jmoiron/sqlx"
"github.com/jrockway/jsso2/pkg/internalauth"
"github.com/jrockway/jsso2/pkg/jssopb"
"github.com/jrockway/jsso2/pkg/sessions"
"github.com/jrockway/jsso2/pkg/store"
"github.com/jrockway/jsso2/pkg/types"
"github.com/jrockway/jsso2/pkg/web"
"github.com/jrockway/jsso2/pkg/webauthn"
"go.uber.org/zap"
"google.golang.org/protobuf/types/known/timestamppb"
)
type Service struct {
DB *store.Connection
Permissions *internalauth.Permissions
Linker *web.Linker
Webauthn *webauthn.Config
}
func (s *Service) Start(ctx context.Context, req *jssopb.StartEnrollmentRequest) (*jssopb.StartEnrollmentReply, error) {
reply := &jssopb.StartEnrollmentReply{}
session := sessions.MustFromContext(ctx)
if err := s.Permissions.AllowStartEnrollment(ctx, session); err != nil {
return reply, fmt.Errorf("check permissions: %w", err)
}
user := session.GetUser()
reply.User = user
l := ctxzap.Extract(ctx)
var creds []*types.Credential
if err := s.DB.DoTx(ctx, l, true, func(tx *sqlx.Tx) error {
c, err := store.GetUserCredentials(ctx, tx, user)
if err != nil {
return fmt.Errorf("get credentials for user %s: %w", user.GetUsername(), err)
}
creds = c
return nil
}); err != nil {
return reply, store.AsGRPCError(err)
}
opts, err := s.Webauthn.BeginEnrollment(session, creds)
if err != nil {
return reply, fmt.Errorf("create challenge: %w", err)
}
reply.CredentialCreationOptions = opts
return reply, nil
}
func (s *Service) Finish(ctx context.Context, req *jssopb.FinishEnrollmentRequest) (*jssopb.FinishEnrollmentReply, error) {
reply := &jssopb.FinishEnrollmentReply{}
session := sessions.MustFromContext(ctx)
if err := s.Permissions.AllowFinishEnrollment(ctx, session); err != nil {
return reply, fmt.Errorf("check permissions: %w", err)
}
credential, err := s.Webauthn.FinishEnrollment(session, req)
if err != nil {
return reply, fmt.Errorf("validate credential: %w", err)
}
credential.Id = 0
credential.Name = strings.TrimSpace(req.GetName())
credential.User = session.GetUser()
credential.CreatedAt = timestamppb.Now()
credential.CreatedBySessionId = session.GetId()
l := ctxzap.Extract(ctx)
if err := s.DB.DoTx(ctx, l, false, func(tx *sqlx.Tx) error {
if err := store.AddCredential(ctx, tx, credential); err != nil {
return fmt.Errorf("add credential: %w", err)
}
s, err := store.LookupSession(ctx, tx, session.GetId())
if err != nil {
return fmt.Errorf("lookup session: %w", err)
}
if sessions.HasTaint(s, sessions.TaintEnrollment) {
s.ExpiresAt = timestamppb.Now()
if err := store.UpdateSession(ctx, tx, s); err != nil {
return fmt.Errorf("expire session: %w", err)
}
}
return nil
}); err != nil {
return reply, store.AsGRPCError(err)
}
l.Debug("enrolled new credential", zap.Binary("credential_id", credential.GetCredentialId()))
reply.LoginUrl = s.Linker.LoginPage()
return reply, nil
}