1package models
2
3import (
4 "context"
5 "time"
6
7 "github.com/Azure/go-autorest/autorest/to"
8 "github.com/bluesky-social/indigo/atproto/atcrypto"
9)
10
11type TwoFactorType string
12
13var (
14 TwoFactorTypeNone = TwoFactorType("none")
15 TwoFactorTypeEmail = TwoFactorType("email")
16)
17
18type Repo struct {
19 Did string `gorm:"primaryKey"`
20 CreatedAt time.Time
21 Email string `gorm:"uniqueIndex"`
22 EmailConfirmedAt *time.Time
23 EmailVerificationCode *string
24 EmailVerificationCodeExpiresAt *time.Time
25 EmailUpdateCode *string
26 EmailUpdateCodeExpiresAt *time.Time
27 PasswordResetCode *string
28 PasswordResetCodeExpiresAt *time.Time
29 PlcOperationCode *string
30 PlcOperationCodeExpiresAt *time.Time
31 AccountDeleteCode *string
32 AccountDeleteCodeExpiresAt *time.Time
33 Password string
34 SigningKey []byte
35 Rev string
36 Root []byte
37 Preferences []byte
38 Deactivated bool
39 TwoFactorCode *string
40 TwoFactorCodeExpiresAt *time.Time
41 TwoFactorType TwoFactorType `gorm:"default:none"`
42}
43
44func (r *Repo) SignFor(ctx context.Context, did string, msg []byte) ([]byte, error) {
45 k, err := atcrypto.ParsePrivateBytesK256(r.SigningKey)
46 if err != nil {
47 return nil, err
48 }
49
50 sig, err := k.HashAndSign(msg)
51 if err != nil {
52 return nil, err
53 }
54
55 return sig, nil
56}
57
58func (r *Repo) Status() *string {
59 var status *string
60 if r.Deactivated {
61 status = to.StringPtr("deactivated")
62 }
63 return status
64}
65
66func (r *Repo) Active() bool {
67 return r.Status() == nil
68}
69
70type Actor struct {
71 Did string `gorm:"primaryKey"`
72 Handle string `gorm:"uniqueIndex"`
73}
74
75type RepoActor struct {
76 Repo
77 Actor
78}
79
80type InviteCode struct {
81 Code string `gorm:"primaryKey"`
82 Did string `gorm:"index"`
83 RemainingUseCount int
84}
85
86type Token struct {
87 Token string `gorm:"primaryKey"`
88 Did string `gorm:"index"`
89 RefreshToken string `gorm:"index"`
90 CreatedAt time.Time
91 ExpiresAt time.Time `gorm:"index:,sort:asc"`
92}
93
94type RefreshToken struct {
95 Token string `gorm:"primaryKey"`
96 Did string `gorm:"index"`
97 CreatedAt time.Time
98 ExpiresAt time.Time `gorm:"index:,sort:asc"`
99}
100
101type Record struct {
102 Did string `gorm:"primaryKey:idx_record_did_created_at;index:idx_record_did_nsid"`
103 CreatedAt string `gorm:"index;index:idx_record_did_created_at,sort:desc"`
104 Nsid string `gorm:"primaryKey;index:idx_record_did_nsid"`
105 Rkey string `gorm:"primaryKey"`
106 Cid string
107 Value []byte
108}
109
110type Block struct {
111 Did string `gorm:"primaryKey;index:idx_blocks_by_rev"`
112 Cid []byte `gorm:"primaryKey"`
113 Rev string `gorm:"index:idx_blocks_by_rev,sort:desc"`
114 Value []byte
115}
116
117type Blob struct {
118 ID uint
119 CreatedAt string `gorm:"index"`
120 Did string `gorm:"index;index:idx_blob_did_cid"`
121 Cid []byte `gorm:"index;index:idx_blob_did_cid"`
122 RefCount int
123 Storage string `gorm:"default:sqlite"`
124}
125
126type BlobPart struct {
127 Blob Blob
128 BlobID uint `gorm:"primaryKey"`
129 Idx int `gorm:"primaryKey"`
130 Data []byte
131}
132
133type ReservedKey struct {
134 KeyDid string `gorm:"primaryKey"`
135 Did *string `gorm:"index"`
136 PrivateKey []byte
137 CreatedAt time.Time `gorm:"index"`
138}