-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession.go
More file actions
118 lines (96 loc) · 2.61 KB
/
Copy pathsession.go
File metadata and controls
118 lines (96 loc) · 2.61 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package ctapkit
import (
"context"
"errors"
rtdevice "github.com/go-ctap/kit/internal/device"
rtsession "github.com/go-ctap/kit/internal/session"
"github.com/go-ctap/kit/model"
)
type OpenSessionOption func(*openSessionConfig)
type openSessionConfig struct {
events model.EventSink
strictPermissions bool
}
type RunOption func(*runConfig)
type runConfig struct {
verificationFlow model.VerificationFlow
}
func WithEventSink(events model.EventSink) OpenSessionOption {
return func(config *openSessionConfig) {
config.events = events
}
}
// WithStrictPermissions scopes credential management mutation tokens to the
// target RP ID for credentials.delete and credentials.updateUser operations.
func WithStrictPermissions() OpenSessionOption {
return func(config *openSessionConfig) {
config.strictPermissions = true
}
}
func WithVerificationFlow(flow model.VerificationFlow) RunOption {
return func(config *runConfig) {
config.verificationFlow = flow
}
}
type Session struct {
core *rtsession.Core
}
type sessionDependencies struct {
openAuthenticator authenticatorOpenFunc
}
func OpenSession(ctx context.Context, dev Device, opts ...OpenSessionOption) (*Session, error) {
return openSession(ctx, dev, defaultSessionDependencies(), opts...)
}
func defaultSessionDependencies() sessionDependencies {
return sessionDependencies{
openAuthenticator: openAuthenticator,
}
}
func openSession(
ctx context.Context,
dev Device,
deps sessionDependencies,
opts ...OpenSessionOption,
) (*Session, error) {
config := &openSessionConfig{
events: model.NoopEventSink{},
}
for _, opt := range opts {
if opt != nil {
opt(config)
}
}
if config.events == nil {
config.events = model.NoopEventSink{}
}
if !dev.valid {
return nil, model.NewRuntimeError(model.ErrorInvalidOperation, "device handle is invalid", nil)
}
selected := dev.report
lease, err := rtdevice.AcquireLease(ctx, selected)
if err != nil {
return nil, runtimeDeviceError(err)
}
authenticator, err := deps.openAuthenticator(ctx, selected.Transport, selected.Path)
if err != nil {
releaseErr := lease.Close()
return nil, errors.Join(runtimeDeviceError(err), releaseErr)
}
return &Session{
core: rtsession.New(lease, selected, authenticator, config.events, config.strictPermissions),
}, nil
}
func (s *Session) Run(
ctx context.Context,
operation model.Operation,
handler model.InteractionHandler,
opts ...RunOption,
) (model.OperationResult, error) {
return s.run(ctx, operation, handler, opts...)
}
func (s *Session) Close() error {
return s.core.Close()
}
func (s *Session) Info() model.SessionInfo {
return s.core.Info()
}