-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathexternal_auth_model.go
More file actions
178 lines (144 loc) · 4.86 KB
/
external_auth_model.go
File metadata and controls
178 lines (144 loc) · 4.86 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
package core
import (
"context"
"errors"
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/pocketbase/pocketbase/tools/auth"
"github.com/pocketbase/pocketbase/tools/hook"
"github.com/pocketbase/pocketbase/tools/types"
)
var (
_ Model = (*ExternalAuth)(nil)
_ PreValidator = (*ExternalAuth)(nil)
_ RecordProxy = (*ExternalAuth)(nil)
)
const CollectionNameExternalAuths = "_externalAuths"
// ExternalAuth defines a Record proxy for working with the externalAuths collection.
type ExternalAuth struct {
*Record
}
// NewExternalAuth instantiates and returns a new blank *ExternalAuth model.
//
// Example usage:
//
// ea := core.NewExternalAuth(app)
// ea.SetRecordRef(user.Id)
// ea.SetCollectionRef(user.Collection().Id)
// ea.SetProvider("google")
// ea.SetProviderId("...")
// app.Save(ea)
func NewExternalAuth(app App) *ExternalAuth {
m := &ExternalAuth{}
c, err := app.FindCachedCollectionByNameOrId(CollectionNameExternalAuths)
if err != nil {
// this is just to make tests easier since it is a system collection and it is expected to be always accessible
// (note: the loaded record is further checked on ExternalAuth.PreValidate())
c = NewBaseCollection("@__invalid__")
}
m.Record = NewRecord(c)
return m
}
// PreValidate implements the [PreValidator] interface and checks
// whether the proxy is properly loaded.
func (m *ExternalAuth) PreValidate(ctx context.Context, app App) error {
if m.Record == nil || m.Record.Collection().Name != CollectionNameExternalAuths {
return errors.New("missing or invalid ExternalAuth ProxyRecord")
}
return nil
}
// ProxyRecord returns the proxied Record model.
func (m *ExternalAuth) ProxyRecord() *Record {
return m.Record
}
// SetProxyRecord loads the specified record model into the current proxy.
func (m *ExternalAuth) SetProxyRecord(record *Record) {
m.Record = record
}
// CollectionRef returns the "collectionRef" field value.
func (m *ExternalAuth) CollectionRef() string {
return m.GetString("collectionRef")
}
// SetCollectionRef updates the "collectionRef" record field value.
func (m *ExternalAuth) SetCollectionRef(collectionId string) {
m.Set("collectionRef", collectionId)
}
// RecordRef returns the "recordRef" record field value.
func (m *ExternalAuth) RecordRef() string {
return m.GetString("recordRef")
}
// SetRecordRef updates the "recordRef" record field value.
func (m *ExternalAuth) SetRecordRef(recordId string) {
m.Set("recordRef", recordId)
}
// Provider returns the "provider" record field value.
func (m *ExternalAuth) Provider() string {
return m.GetString("provider")
}
// SetProvider updates the "provider" record field value.
func (m *ExternalAuth) SetProvider(provider string) {
m.Set("provider", provider)
}
// Provider returns the "providerId" record field value.
func (m *ExternalAuth) ProviderId() string {
return m.GetString("providerId")
}
// SetProvider updates the "providerId" record field value.
func (m *ExternalAuth) SetProviderId(providerId string) {
m.Set("providerId", providerId)
}
// Created returns the "created" record field value.
func (m *ExternalAuth) Created() types.DateTime {
return m.GetDateTime("created")
}
// Updated returns the "updated" record field value.
func (m *ExternalAuth) Updated() types.DateTime {
return m.GetDateTime("updated")
}
func (app *BaseApp) registerExternalAuthHooks() {
recordRefHooks[*ExternalAuth](app, CollectionNameExternalAuths, CollectionTypeAuth)
app.OnRecordValidate(CollectionNameExternalAuths).Bind(&hook.Handler[*RecordEvent]{
Func: func(e *RecordEvent) error {
providerNames := make([]any, 0, len(auth.Providers))
for name := range auth.Providers {
providerNames = append(providerNames, name)
}
provider := e.Record.GetString("provider")
if err := validation.Validate(provider, validation.Required, validation.In(providerNames...)); err != nil {
return validation.Errors{"provider": err}
}
return e.Next()
},
Priority: 99,
})
// delete all pre-existing external auths on verified upgrade
app.OnRecordUpdateExecute().Bind(&hook.Handler[*RecordEvent]{
Func: func(e *RecordEvent) error {
if !e.Record.Collection().IsAuth() {
return e.Next()
}
hasUpgradedVerified := !e.Record.Original().IsNew() && !e.Record.Original().Verified() && e.Record.Verified()
if !hasUpgradedVerified {
return e.Next()
}
originalApp := e.App
return e.App.RunInTransaction(func(txApp App) error {
e.App = txApp
defer func() { e.App = originalApp }()
externalAuths, err := txApp.FindAllExternalAuthsByRecord(e.Record)
if err != nil {
return err
}
if len(externalAuths) > 0 {
// delete all pre-existing external auths
if err := txApp.DeleteAllExternalAuthsByRecord(e.Record); err != nil {
return err
}
// force refresh tokens reset (if not already)
e.Record.RefreshTokenKey()
}
return e.Next()
})
},
Priority: 99,
})
}