This repository was archived by the owner on Oct 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate.go
More file actions
148 lines (139 loc) · 4 KB
/
migrate.go
File metadata and controls
148 lines (139 loc) · 4 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
package core
import (
"bufio"
"context"
"fmt"
"github.com/identityOrg/cerberus-core/models"
"github.com/identityOrg/oidcsdk"
"gopkg.in/square/go-jose.v2"
"gorm.io/gorm"
"os"
"strings"
)
func SetupDemoData(ormDB *gorm.DB, config *Config, sdkConfig *oidcsdk.Config, redirectUri string) error {
fmt.Println("Creating demo client with client_id=client and client_secret=client")
spMetadata := &models.ServiceProviderMetadata{
RedirectUris: []string{sdkConfig.Issuer + "/redirect"},
Scopes: strings.Split("openid|offline|offline_access", "|"),
GrantTypes: strings.Split("authorization_code|password|refresh_token|client_credentials|implicit", "|"),
ApplicationType: "web",
IdTokenSignedResponseAlg: string(jose.RS256),
}
if redirectUri != "" {
spMetadata.RedirectUris = append(spMetadata.RedirectUris, redirectUri)
}
enc := NewNoOpTextEncrypt()
spService := NewSPStoreServiceImpl(ormDB, enc, enc)
existingSP, err := spService.FindSPByClientId(context.Background(), "client")
if err != nil {
spId, err := spService.CreateSP(context.Background(), "Demo Client", "Demo Client", spMetadata)
if err != nil {
return err
}
existingSP, err = spService.GetSP(context.Background(), spId)
if err != nil {
return err
}
}
existingSP.ClientID = "client"
existingSP.ClientSecret = "client"
existingSP.Public = false
err = ormDB.Save(existingSP).Error
if err != nil {
return err
}
fmt.Println("Creating demo user with username=user and password=user")
userService := NewUserStoreServiceImpl(ormDB, config)
metadata := &models.UserMetadata{}
metadata.SetName("Demo User")
metadata.SetEmail("user@demo.com")
metadata.SetEmailVerified(true)
ctx := context.Background()
var uid uint
user, err := userService.FindUserByUsername(ctx, "user")
if err != nil {
uid, err = userService.CreateUser(ctx, "user", "user@demo.com", metadata)
if err != nil {
return err
}
} else {
uid = user.ID
}
err = userService.UpdateUser(ctx, uid, metadata)
if err != nil {
return err
}
err = userService.SetPassword(ctx, uid, "user")
if err != nil {
return err
}
err = userService.ActivateUser(ctx, uid)
if err != nil {
return err
}
fmt.Println("Creating default secret key")
secretStore := NewSecretStoreServiceImpl(ormDB)
_, err = secretStore.GetChannelByAlgoUse(nil, "RS256", "sig")
if err != nil {
_, err = secretStore.CreateChannel(nil, "default", "RS256", "sig", 30)
}
return err
}
func SetupDBStructure(ormDB *gorm.DB, drop bool, force bool) error {
if drop && !force {
fmt.Printf("Do you want to continue (Y/n): ")
reader := bufio.NewReader(os.Stdin)
char, _, err := reader.ReadRune()
if err != nil {
return err
} else {
switch char {
case 'Y':
case 'y':
fmt.Println("continuing the migration with drop table")
default:
fmt.Println("Aborting the migration")
return nil
}
}
}
scopeT := &models.ScopeModel{}
claimT := &models.ClaimModel{}
channelT := &models.SecretChannelModel{}
secretT := &models.SecretModel{}
userT := &models.UserModel{}
credentialsT := &models.UserCredentials{}
otpT := &models.UserOTP{}
spT := &models.ServiceProviderModel{}
tokensT := &models.TokensModel{}
jtiT := &models.JTIModel{}
tables := []dbTable{scopeT, claimT, channelT, secretT, userT, credentialsT, otpT, spT, tokensT, jtiT}
fmt.Println("dropping all tables")
if drop {
for _, table := range tables {
err := ormDB.Migrator().DropTable(table)
if err != nil {
return fmt.Errorf("error dropping table %s:%v", table.TableName(), err)
}
}
}
fmt.Println("creating all tables")
for _, table := range tables {
var err error
if ma, ok := table.(MigrateAware); ok {
err = ma.AutoMigrate(ormDB.Migrator())
} else {
err = ormDB.AutoMigrate(table)
}
if err != nil {
return fmt.Errorf("error creating table %s:%v", table.TableName(), err)
}
}
return InitializeDefaultScope(ormDB)
}
type dbTable interface {
TableName() string
}
type MigrateAware interface {
AutoMigrate(db gorm.Migrator) error
}