generated from datumforge/go-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
apitoken.go
146 lines (133 loc) · 3.33 KB
/
apitoken.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
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
package schema
import (
"context"
"entgo.io/contrib/entgql"
"entgo.io/contrib/entoas"
"entgo.io/ent"
"entgo.io/ent/schema"
"entgo.io/ent/schema/field"
"entgo.io/ent/schema/index"
"github.com/datumforge/enthistory"
emixin "github.com/datumforge/entx/mixin"
"github.com/datumforge/fgax/entfga"
"github.com/datumforge/datum/internal/ent/generated"
"github.com/datumforge/datum/internal/ent/generated/privacy"
"github.com/datumforge/datum/internal/ent/hooks"
"github.com/datumforge/datum/internal/ent/interceptors"
"github.com/datumforge/datum/internal/ent/mixin"
"github.com/datumforge/datum/pkg/keygen"
)
// APIToken holds the schema definition for the APIToken entity.
type APIToken struct {
ent.Schema
}
// Fields of the APIToken
func (APIToken) Fields() []ent.Field {
return []ent.Field{
field.String("name").
Comment("the name associated with the token").
NotEmpty(),
field.String("token").
Unique().
Immutable().
Annotations(
entgql.Skip(^entgql.SkipType),
entoas.Skip(true),
).
DefaultFunc(func() string {
token := keygen.PrefixedSecret("dtma") // datum api token prefix
return token
}),
field.Time("expires_at").
Comment("when the token expires").
Annotations(
entgql.Skip(entgql.SkipMutationUpdateInput),
).
Optional(),
field.String("description").
Comment("a description of the token's purpose").
Optional().
Nillable().
Annotations(
entgql.Skip(entgql.SkipWhereInput),
),
field.JSON("scopes", []string{}).
Optional(),
field.Time("last_used_at").
Optional().
Nillable(),
}
}
// Edges of the APIToken
func (APIToken) Edges() []ent.Edge {
return []ent.Edge{}
}
// Indexes of the APIToken
func (APIToken) Indexes() []ent.Index {
return []ent.Index{
// non-unique index.
index.Fields("token"),
}
}
// Mixin of the APIToken
func (APIToken) Mixin() []ent.Mixin {
return []ent.Mixin{
emixin.AuditMixin{},
mixin.SoftDeleteMixin{},
emixin.IDMixin{},
emixin.TagMixin{},
OrgOwnerMixin{
Ref: "api_tokens",
AllowWhere: true,
SkipInterceptor: interceptors.SkipOnlyQuery,
},
}
}
// Annotations of the APIToken
func (APIToken) Annotations() []schema.Annotation {
return []schema.Annotation{
entgql.QueryField(),
entgql.RelayConnection(),
entgql.Mutations(entgql.MutationCreate(), (entgql.MutationUpdate())),
enthistory.Annotations{
Exclude: true,
},
entfga.Annotations{
ObjectType: "organization",
IncludeHooks: false,
NillableIDField: true,
OrgOwnedField: true,
IDField: "OwnerID",
},
}
}
// Hooks of the APIToken
func (APIToken) Hooks() []ent.Hook {
return []ent.Hook{
hooks.HookCreateAPIToken(),
hooks.HookUpdateAPIToken(),
}
}
// Interceptors of the APIToken
func (APIToken) Interceptors() []ent.Interceptor {
return []ent.Interceptor{
interceptors.InterceptorAPIToken(),
}
}
// Policy of the APIToken
func (APIToken) Policy() ent.Policy {
return privacy.Policy{
Mutation: privacy.MutationPolicy{
privacy.APITokenMutationRuleFunc(func(ctx context.Context, am *generated.APITokenMutation) error {
return am.CheckAccessForEdit(ctx)
}),
privacy.AlwaysDenyRule(),
},
Query: privacy.QueryPolicy{
privacy.APITokenQueryRuleFunc(func(ctx context.Context, q *generated.APITokenQuery) error {
return q.CheckAccess(ctx)
}),
privacy.AlwaysDenyRule(),
},
}
}