aboutsummaryrefslogtreecommitdiffstats
path: root/bridge
diff options
context:
space:
mode:
Diffstat (limited to 'bridge')
-rw-r--r--bridge/core/token.go129
1 files changed, 97 insertions, 32 deletions
diff --git a/bridge/core/token.go b/bridge/core/token.go
index 89c70af2..bd97cd18 100644
--- a/bridge/core/token.go
+++ b/bridge/core/token.go
@@ -1,6 +1,8 @@
package core
import (
+ "crypto/sha256"
+ "encoding/json"
"fmt"
"regexp"
"strings"
@@ -10,12 +12,14 @@ import (
const (
tokenConfigKeyPrefix = "git-bug.token"
- tokenKeyTarget = "target"
- tokenKeyScopes = "scopes"
+ tokenValueKey = "value"
+ tokenTargetKey = "target"
+ tokenScopesKey = "scopes"
)
// Token holds an API access token data
type Token struct {
+ ID string
Value string
Target string
Global bool
@@ -24,16 +28,46 @@ type Token struct {
// NewToken instantiate a new token
func NewToken(value, target string, global bool, scopes []string) *Token {
- return &Token{
+ token := &Token{
Value: value,
Target: target,
Global: global,
Scopes: scopes,
}
+
+ token.ID = hashToken(token)
+ return token
+}
+
+// Id return full token identifier. It will compute the Id if it's empty
+func (t *Token) Id() string {
+ if t.ID == "" {
+ t.ID = hashToken(t)
+ }
+
+ return t.ID
+}
+
+// HumanId return the truncated token id
+func (t *Token) HumanId() string {
+ return t.Id()[:6]
+}
+
+func hashToken(token *Token) string {
+ tokenJson, err := json.Marshal(&token)
+ if err != nil {
+ panic(err)
+ }
+
+ sum := sha256.Sum256(tokenJson)
+ return fmt.Sprintf("%x", sum)
}
// Validate ensure token important fields are valid
func (t *Token) Validate() error {
+ if t.ID == "" {
+ return fmt.Errorf("missing id")
+ }
if t.Value == "" {
return fmt.Errorf("missing value")
}
@@ -46,8 +80,17 @@ func (t *Token) Validate() error {
return nil
}
-func loadToken(repo repository.RepoConfig, value string, global bool) (*Token, error) {
- keyPrefix := fmt.Sprintf("git-bug.token.%s.", value)
+// Kind return the type of the token as string
+func (t *Token) Kind() string {
+ if t.Global {
+ return "global"
+ }
+
+ return "local"
+}
+
+func loadToken(repo repository.RepoConfig, id string, global bool) (*Token, error) {
+ keyPrefix := fmt.Sprintf("git-bug.token.%s.", id)
readerFn := repo.ReadConfigs
if global {
@@ -62,19 +105,25 @@ func loadToken(repo repository.RepoConfig, value string, global bool) (*Token, e
// trim key prefix
for key, value := range configs {
+ delete(configs, key)
newKey := strings.TrimPrefix(key, keyPrefix)
configs[newKey] = value
- delete(configs, key)
}
var ok bool
- token := &Token{Value: value, Global: global}
- token.Target, ok = configs[tokenKeyTarget]
+ token := &Token{ID: id, Global: global}
+
+ token.Value, ok = configs[tokenValueKey]
+ if !ok {
+ return nil, fmt.Errorf("empty token value")
+ }
+
+ token.Target, ok = configs[tokenTargetKey]
if !ok {
return nil, fmt.Errorf("empty token key")
}
- scopesString, ok := configs[tokenKeyScopes]
+ scopesString, ok := configs[tokenScopesKey]
if !ok {
return nil, fmt.Errorf("missing scopes config")
}
@@ -84,13 +133,13 @@ func loadToken(repo repository.RepoConfig, value string, global bool) (*Token, e
}
// GetToken loads a token from repo config
-func GetToken(repo repository.RepoConfig, value string) (*Token, error) {
- return loadToken(repo, value, false)
+func GetToken(repo repository.RepoConfig, id string) (*Token, error) {
+ return loadToken(repo, id, false)
}
// GetGlobalToken loads a token from the global config
-func GetGlobalToken(repo repository.RepoConfig, value string) (*Token, error) {
- return loadToken(repo, value, true)
+func GetGlobalToken(repo repository.RepoConfig, id string) (*Token, error) {
+ return loadToken(repo, id, true)
}
func listTokens(repo repository.RepoConfig, global bool) ([]string, error) {
@@ -131,14 +180,29 @@ func listTokens(repo repository.RepoConfig, global bool) ([]string, error) {
return result, nil
}
-// ListTokens return the list of stored tokens in the repo config
-func ListTokens(repo repository.RepoConfig) ([]string, error) {
- return listTokens(repo, false)
-}
+// ListTokens return a map representing the stored tokens in the repo config and global config
+// along with their type (global: true, local:false)
+func ListTokens(repo repository.RepoConfig) (map[string]bool, error) {
+ localTokens, err := listTokens(repo, false)
+ if err != nil {
+ return nil, err
+ }
-// ListGlobalTokens return the list of stored tokens in the global config
-func ListGlobalTokens(repo repository.RepoConfig) ([]string, error) {
- return listTokens(repo, true)
+ globalTokens, err := listTokens(repo, true)
+ if err != nil {
+ return nil, err
+ }
+
+ tokens := map[string]bool{}
+ for _, token := range localTokens {
+ tokens[token] = false
+ }
+
+ for _, token := range globalTokens {
+ tokens[token] = true
+ }
+
+ return tokens, nil
}
func storeToken(repo repository.RepoConfig, token *Token) error {
@@ -147,13 +211,19 @@ func storeToken(repo repository.RepoConfig, token *Token) error {
storeFn = repo.StoreGlobalConfig
}
- storeTargetKey := fmt.Sprintf("git-bug.token.%s.%s", token.Value, tokenKeyTarget)
- err := storeFn(storeTargetKey, token.Target)
+ storeValueKey := fmt.Sprintf("git-bug.token.%s.%s", token.Id(), tokenValueKey)
+ err := storeFn(storeValueKey, token.Value)
+ if err != nil {
+ return err
+ }
+
+ storeTargetKey := fmt.Sprintf("git-bug.token.%s.%s", token.Id(), tokenTargetKey)
+ err = storeFn(storeTargetKey, token.Target)
if err != nil {
return err
}
- storeScopesKey := fmt.Sprintf("git-bug.token.%s.%s", token.Value, tokenKeyScopes)
+ storeScopesKey := fmt.Sprintf("git-bug.token.%s.%s", token.Id(), tokenScopesKey)
return storeFn(storeScopesKey, strings.Join(token.Scopes, ","))
}
@@ -162,19 +232,14 @@ func StoreToken(repo repository.RepoConfig, token *Token) error {
return storeToken(repo, token)
}
-// StoreGlobalToken stores a token in global config
-func StoreGlobalToken(repo repository.RepoConfig, token *Token) error {
- return storeToken(repo, token)
-}
-
// RemoveToken removes a token from the repo config
-func RemoveToken(repo repository.RepoConfig, value string) error {
- keyPrefix := fmt.Sprintf("git-bug.token.%s", value)
+func RemoveToken(repo repository.RepoConfig, id string) error {
+ keyPrefix := fmt.Sprintf("git-bug.token.%s", id)
return repo.RmConfigs(keyPrefix)
}
// RemoveGlobalToken removes a token from the repo config
-func RemoveGlobalToken(repo repository.RepoConfig, value string) error {
- keyPrefix := fmt.Sprintf("git-bug.token.%s", value)
+func RemoveGlobalToken(repo repository.RepoConfig, id string) error {
+ keyPrefix := fmt.Sprintf("git-bug.token.%s", id)
return repo.RmGlobalConfigs(keyPrefix)
}