Repository navigation
Expand file tree
/
Copy pathplugin.go
More file actions
228 lines (200 loc) · 6.7 KB
/
Copy pathplugin.go
File metadata and controls
228 lines (200 loc) · 6.7 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Package deviceverify provides new device detection and challenge-response
// verification. When a user signs in from an unrecognized device, the plugin
// can flag it or require verification via email/SMS.
package deviceverify
import (
"context"
"crypto/rand"
"encoding/hex"
"time"
log "github.com/xraph/go-utils/log"
"github.com/xraph/authsome/bridge"
"github.com/xraph/authsome/device"
"github.com/xraph/authsome/id"
"github.com/xraph/authsome/plugin"
"github.com/xraph/authsome/session"
"github.com/xraph/authsome/settings"
"github.com/xraph/authsome/store"
"github.com/xraph/authsome/user"
)
// Compile-time interface checks.
var (
_ plugin.Plugin = (*Plugin)(nil)
_ plugin.OnInit = (*Plugin)(nil)
_ plugin.AfterSignIn = (*Plugin)(nil)
_ plugin.SettingsProvider = (*Plugin)(nil)
)
// ──────────────────────────────────────────────────
// Dynamic setting definitions
// ──────────────────────────────────────────────────
var (
// SettingNotifyOnNewDevice controls whether a notification is sent when
// a new device is detected.
SettingNotifyOnNewDevice = settings.Define("deviceverify.notify_on_new_device", true,
settings.WithDisplayName("Notify on New Device"),
settings.WithDescription("Send a notification when a new device is detected"),
settings.WithCategory("Device Verification"),
settings.WithScopes(settings.ScopeGlobal, settings.ScopeApp),
settings.WithHelpText("Send a notification when a new device is detected"),
settings.WithOrder(10),
)
// SettingChallengeTTLMinutes controls how long a device verification
// challenge is valid, in minutes.
SettingChallengeTTLMinutes = settings.Define("deviceverify.challenge_ttl_minutes", 10,
settings.WithDisplayName("Challenge TTL (minutes)"),
settings.WithDescription("How long a device verification challenge is valid, in minutes"),
settings.WithCategory("Device Verification"),
settings.WithScopes(settings.ScopeGlobal, settings.ScopeApp),
settings.WithHelpText("How long a device verification challenge is valid, in minutes"),
settings.WithOrder(20),
)
)
// Config configures the device verification plugin.
type Config struct {
// NotifyOnNewDevice sends a notification when a new device is detected
// (default: true).
NotifyOnNewDevice bool
// ChallengeTTL is how long a device verification challenge is valid
// (default: 10m).
ChallengeTTL time.Duration
}
func (c *Config) defaults() {
if c.ChallengeTTL == 0 {
c.ChallengeTTL = 10 * time.Minute
}
}
// Plugin detects new devices on sign-in and optionally sends notifications.
type Plugin struct {
config Config
store store.Store
herald bridge.Herald
chronicle bridge.Chronicle
relay bridge.EventRelay
logger log.Logger
settingsMgr *settings.Manager
}
// New creates a new device verification plugin.
func New(cfg ...Config) *Plugin {
var c Config
if len(cfg) > 0 {
c = cfg[0]
} else {
c.NotifyOnNewDevice = true
}
c.defaults()
return &Plugin{config: c}
}
// Name returns the plugin name.
func (p *Plugin) Name() string { return "deviceverify" }
// DeclareSettings implements plugin.SettingsProvider.
// It registers all device-verification-related configurable settings.
func (p *Plugin) DeclareSettings(m *settings.Manager) error {
if err := settings.RegisterTyped(m, "deviceverify", SettingNotifyOnNewDevice); err != nil {
return err
}
return settings.RegisterTyped(m, "deviceverify", SettingChallengeTTLMinutes)
}
// OnInit discovers engine dependencies.
func (p *Plugin) OnInit(_ context.Context, engine plugin.Engine) error {
p.logger = engine.Logger()
if p.logger == nil {
p.logger = log.NewNoopLogger()
}
p.store = engine.Store()
p.herald = engine.Herald()
p.chronicle = bridge.NewBusChronicle(engine.Hooks())
p.relay = engine.Relay()
p.settingsMgr = engine.Settings()
return nil
}
// OnAfterSignIn checks if the device used for sign-in is new/unrecognized.
func (p *Plugin) OnAfterSignIn(ctx context.Context, u *user.User, s *session.Session) error {
if p.store == nil {
return nil
}
// If no device was bound to the session, nothing to verify.
if s.DeviceID.Prefix() == "" {
return nil
}
dev, err := p.store.GetDevice(ctx, s.DeviceID)
if err != nil {
return nil // can't verify without the device record
}
// A device is "new" if it was just created (CreatedAt ≈ now).
if time.Since(dev.CreatedAt) > 30*time.Second {
// Device existed before — not new.
return nil
}
p.handleNewDevice(ctx, u, s, dev)
return nil
}
func (p *Plugin) handleNewDevice(ctx context.Context, u *user.User, _ *session.Session, dev *device.Device) {
p.logger.Info("deviceverify: new device detected",
log.String("user_id", u.ID.String()),
log.String("device_id", dev.ID.String()),
log.String("browser", dev.Browser),
log.String("os", dev.OS),
)
if p.chronicle != nil {
_ = p.chronicle.Record(ctx, &bridge.AuditEvent{ //nolint:errcheck // best-effort audit
Action: "new_device_detected",
Resource: "device",
ResourceID: dev.ID.String(),
ActorID: u.ID.String(),
Tenant: u.AppID.String(),
Outcome: bridge.OutcomeSuccess,
Severity: bridge.SeverityInfo,
Metadata: map[string]string{
"browser": dev.Browser,
"os": dev.OS,
"ip_address": dev.IPAddress,
"type": dev.Type,
},
})
}
if p.relay != nil {
_ = p.relay.Send(ctx, &bridge.WebhookEvent{ //nolint:errcheck // best-effort webhook
Type: "device.new_detected",
TenantID: u.AppID.String(),
Data: map[string]string{
"user_id": u.ID.String(),
"device_id": dev.ID.String(),
"browser": dev.Browser,
"os": dev.OS,
},
})
}
// Send notification to user about the new device.
if p.config.NotifyOnNewDevice && p.herald != nil {
name := u.Name()
if name == "" {
name = u.Email
}
_ = p.herald.Notify(ctx, &bridge.HeraldNotifyRequest{ //nolint:errcheck // best-effort notification
Template: "new-device-login",
Channels: []string{"email"},
To: []string{u.Email},
UserID: u.ID.String(),
Locale: "en",
Async: true,
Data: map[string]any{
"user_name": name,
"browser": dev.Browser,
"os": dev.OS,
"ip_address": dev.IPAddress,
"device_type": dev.Type,
"login_time": time.Now().Format(time.RFC1123),
},
})
}
}
// GenerateToken creates a random hex token for device challenges.
func GenerateToken() (string, error) {
b := make([]byte, 32)
if _, err := rand.Read(b); err != nil {
return "", err
}
return hex.EncodeToString(b), nil
}
// Ensure unused imports are used.
var _ = id.NewDeviceID