2015-04-07 08:20:55 +00:00
|
|
|
package bolt
|
|
|
|
|
|
|
|
import (
|
2015-04-10 19:52:33 +00:00
|
|
|
"github.com/boltdb/bolt"
|
2015-04-13 05:32:32 +00:00
|
|
|
"github.com/drone/drone/common"
|
2015-04-07 08:20:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetToken gets a token by sha value.
|
2015-04-13 05:32:32 +00:00
|
|
|
func (db *DB) GetToken(user, label string) (*common.Token, error) {
|
2015-04-07 08:20:55 +00:00
|
|
|
token := &common.Token{}
|
2015-04-13 05:32:32 +00:00
|
|
|
key := []byte(user + "/" + label)
|
|
|
|
err := db.View(func(t *bolt.Tx) error {
|
2015-04-10 19:52:33 +00:00
|
|
|
return get(t, bucketTokens, key, token)
|
|
|
|
})
|
2015-04-07 08:20:55 +00:00
|
|
|
return token, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// InsertToken inserts a new user token in the datastore.
|
|
|
|
// If the token already exists and error is returned.
|
|
|
|
func (db *DB) InsertToken(token *common.Token) error {
|
2015-04-13 05:32:32 +00:00
|
|
|
key := []byte(token.Login + "/" + token.Label)
|
|
|
|
return db.Update(func(t *bolt.Tx) error {
|
2015-04-13 22:51:15 +00:00
|
|
|
err := push(t, bucketUserTokens, []byte(token.Login), key)
|
2015-04-13 08:22:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-10 19:52:33 +00:00
|
|
|
return insert(t, bucketTokens, key, token)
|
|
|
|
})
|
2015-04-07 08:20:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteUser deletes the token.
|
|
|
|
func (db *DB) DeleteToken(token *common.Token) error {
|
2015-04-13 05:32:32 +00:00
|
|
|
key := []byte(token.Login + "/" + token.Label)
|
|
|
|
return db.Update(func(t *bolt.Tx) error {
|
2015-04-13 22:51:15 +00:00
|
|
|
err := splice(t, bucketUserTokens, []byte(token.Login), key)
|
2015-04-13 08:22:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-10 19:52:33 +00:00
|
|
|
return delete(t, bucketUser, key)
|
|
|
|
})
|
2015-04-07 08:20:55 +00:00
|
|
|
}
|