-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth0.go
141 lines (117 loc) · 3.39 KB
/
auth0.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
package main
import (
"context"
"fmt"
"strings"
"github.com/pixlise/core/v4/api/dbCollections"
"github.com/pixlise/core/v4/core/utils"
protos "github.com/pixlise/core/v4/generated-protos"
"go.mongodb.org/mongo-driver/mongo"
"gopkg.in/auth0.v4/management"
)
func migrateAuth0UserGroups(auth0Domain string, auth0ClientId string, auth0Secret string, dest *mongo.Database) (map[string]string, error) {
result := map[string]string{}
coll := dest.Collection(dbCollections.UserGroupsName)
err := coll.Drop(context.TODO())
if err != nil {
return result, err
}
/*roleToGroup, allGroups, userToGroup, err := readFromAuth0(auth0Domain, auth0ClientId, auth0Secret)
if err != nil {
return err
}*/
_, allGroups, userToGroup := dummyRead()
// Form user groups
for group := range allGroups {
groupMembers := []string{}
// Run through all users, and add the ones that are in this group
for user, groups := range userToGroup {
if utils.ItemInSlice(group, groups) {
groupMembers = append(groupMembers, user)
}
}
group = strings.TrimPrefix(group, "access:")
dbGroup := &protos.UserGroupDB{
Id: makeID(),
Name: group,
//CreatedUnixSec: ,
Viewers: &protos.UserGroupList{
UserIds: []string{},
GroupIds: []string{},
},
Members: &protos.UserGroupList{
UserIds: groupMembers,
GroupIds: []string{},
},
AdminUserIds: []string{},
}
_, err := coll.InsertOne(context.TODO(), dbGroup)
if err != nil {
return result, err
}
// Remember this group
result[group] = dbGroup.Id
}
fmt.Printf("Created %v user groups\n", len(allGroups))
return result, nil
}
func readFromAuth0(auth0Domain string, auth0ClientId string, auth0Secret string) (map[string][]string, map[string]bool, map[string][]string, error) {
roleToGroup := map[string][]string{}
allGroups := map[string]bool{}
userToGroup := map[string][]string{}
api, err := management.New(auth0Domain, auth0ClientId, auth0Secret)
if err != nil {
return roleToGroup, allGroups, userToGroup, err
}
// Get all roles
roles, err := api.Role.List()
if err != nil {
return roleToGroup, allGroups, userToGroup, err
}
// Generate a list of user IDs to groups they belong to
for _, role := range roles.Roles {
roleToGroup[*role.ID] = []string{}
perm, err := api.Role.Permissions(*role.ID)
if err != nil {
return roleToGroup, allGroups, userToGroup, err
}
for _, p := range perm.Permissions {
permName := p.GetName()
if strings.Contains(permName, "access:") {
roleToGroup[*role.ID] = append(roleToGroup[*role.ID], permName)
allGroups[permName] = true
}
}
}
// Find what roles each user has
var userPage int
for {
userList, err := api.User.List(
management.Query(""),
management.Page(userPage),
)
if err != nil {
return roleToGroup, allGroups, userToGroup, err
}
for _, u := range userList.Users {
userId := u.GetID()
gotRoles, err := api.User.Roles(userId)
if err != nil {
return roleToGroup, allGroups, userToGroup, err
}
for _, role := range gotRoles.Roles {
roleId := role.GetID()
if group, ok := roleToGroup[roleId]; !ok {
return roleToGroup, allGroups, userToGroup, fmt.Errorf("User: %v failed to get role: %v", userId, roleId)
} else {
userToGroup[userId] = append(userToGroup[userId], group...)
}
}
}
if !userList.HasNext() {
break
}
userPage++
}
return roleToGroup, allGroups, userToGroup, nil
}