14
14
15
15
// Name of the bucket where we store information about pipelines
16
16
pipelineBucket = []byte ("Pipelines" )
17
+
18
+ // Username and password of the first admin user
19
+ adminUsername = "admin"
20
+ adminPassword = "admin"
17
21
)
18
22
19
23
// Store represents the access type for store
@@ -52,31 +56,50 @@ func (s *Store) UserUpdate(u *gaia.User) error {
52
56
// given password is valid. Returns nil if password was
53
57
// wrong or user not found.
54
58
func (s * Store ) UserAuth (u * gaia.User ) (* gaia.User , error ) {
59
+ // Look up user
60
+ user , err := s .UserGet (u .Username )
61
+
62
+ // Error occured and/or user not found
63
+ if err != nil || user == nil {
64
+ return nil , err
65
+ }
66
+
67
+ // Check if password is valid
68
+ if user .Password != u .Password {
69
+ return nil , nil
70
+ }
71
+
72
+ // We will use the user object later.
73
+ // But we don't need the password anymore.
74
+ user .Password = ""
75
+
76
+ // Return user
77
+ return user , nil
78
+ }
79
+
80
+ // UserGet looks up a user by given username.
81
+ // Returns nil if user was not found.
82
+ func (s * Store ) UserGet (username string ) (* gaia.User , error ) {
55
83
user := & gaia.User {}
56
84
err := s .db .View (func (tx * bolt.Tx ) error {
57
85
// Get bucket
58
86
b := tx .Bucket (userBucket )
59
87
60
88
// Lookup user
61
- userRaw := b .Get ([]byte (u . Username ))
89
+ userRaw := b .Get ([]byte (username ))
62
90
63
91
// User found?
64
92
if userRaw == nil {
65
93
// Nope. That is not an error so just leave
94
+ user = nil
66
95
return nil
67
96
}
68
97
69
98
// Unmarshal
70
99
return json .Unmarshal (userRaw , user )
71
100
})
72
101
73
- // Check if password is valid
74
- if err != nil || u .Password != user .Password {
75
- return nil , err
76
- }
77
-
78
- // Return outcome
79
- return user , nil
102
+ return user , err
80
103
}
81
104
82
105
// Init initalizes the connection to the database.
@@ -89,7 +112,7 @@ func (s *Store) Init(cfg *gaia.Config) error {
89
112
}
90
113
s .db = db
91
114
92
- // Create bucket if not exists
115
+ // Create bucket if not exists function
93
116
var bucketName []byte
94
117
c := func (tx * bolt.Tx ) error {
95
118
_ , err := tx .CreateBucketIfNotExists (bucketName )
@@ -111,5 +134,24 @@ func (s *Store) Init(cfg *gaia.Config) error {
111
134
return err
112
135
}
113
136
137
+ // Make sure that the user "admin" does exist
138
+ admin , err := s .UserGet (adminUsername )
139
+ if err != nil {
140
+ return err
141
+ }
142
+
143
+ // Create admin user if we cannot find it
144
+ if admin == nil {
145
+ err = s .UserUpdate (& gaia.User {
146
+ DisplayName : adminUsername ,
147
+ Username : adminUsername ,
148
+ Password : adminPassword ,
149
+ })
150
+
151
+ if err != nil {
152
+ return err
153
+ }
154
+ }
155
+
114
156
return nil
115
157
}
0 commit comments