aboutsummaryrefslogtreecommitdiffstats
path: root/bridge/core
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2020-01-24 00:30:13 +0100
committerMichael Muré <batolettre@gmail.com>2020-02-08 17:19:57 +0100
commit74e91144105790cc997c1d79a7f638e1e3a1f3f8 (patch)
tree33ef7b3cf547afc10b613e5d4de087ca0439232b /bridge/core
parent8da522d97af3dcaca8a8424e3541705c69779d6f (diff)
downloadgit-bug-74e91144105790cc997c1d79a7f638e1e3a1f3f8.tar.gz
more more wip
Diffstat (limited to 'bridge/core')
-rw-r--r--bridge/core/auth/credential.go6
-rw-r--r--bridge/core/auth/credential_test.go2
-rw-r--r--bridge/core/auth/options.go2
-rw-r--r--bridge/core/auth/token.go13
-rw-r--r--bridge/core/config.go42
5 files changed, 62 insertions, 3 deletions
diff --git a/bridge/core/auth/credential.go b/bridge/core/auth/credential.go
index e843ede7..c1255aa6 100644
--- a/bridge/core/auth/credential.go
+++ b/bridge/core/auth/credential.go
@@ -40,7 +40,10 @@ type Credential interface {
Kind() CredentialKind
CreateTime() time.Time
Validate() error
+
Metadata() map[string]string
+ GetMetadata(key string) (string, bool)
+ SetMetadata(key string, value string)
// Return all the specific properties of the credential that need to be saved into the configuration.
// This does not include Target, Kind, CreateTime and Metadata.
@@ -124,6 +127,9 @@ func metaFromConfig(configs map[string]string) map[string]string {
result[key] = val
}
}
+ if len(result) == 0 {
+ return nil
+ }
return result
}
diff --git a/bridge/core/auth/credential_test.go b/bridge/core/auth/credential_test.go
index 49c138cf..2f8806c9 100644
--- a/bridge/core/auth/credential_test.go
+++ b/bridge/core/auth/credential_test.go
@@ -63,7 +63,7 @@ func TestCredential(t *testing.T) {
// Metadata
- token4.Metadata()["key"] = "value"
+ token4.SetMetadata("key", "value")
err = Store(repo, token4)
assert.NoError(t, err)
diff --git a/bridge/core/auth/options.go b/bridge/core/auth/options.go
index 0c780dc1..74189874 100644
--- a/bridge/core/auth/options.go
+++ b/bridge/core/auth/options.go
@@ -26,7 +26,7 @@ func (opts *options) Match(cred Credential) bool {
}
for key, val := range opts.meta {
- if v, ok := cred.Metadata()[key]; !ok || v != val {
+ if v, ok := cred.GetMetadata(key); !ok || v != val {
return false
}
}
diff --git a/bridge/core/auth/token.go b/bridge/core/auth/token.go
index 60137cd9..42f960bf 100644
--- a/bridge/core/auth/token.go
+++ b/bridge/core/auth/token.go
@@ -30,7 +30,6 @@ func NewToken(value, target string) *Token {
target: target,
createTime: time.Now(),
Value: value,
- meta: make(map[string]string),
}
}
@@ -88,6 +87,18 @@ func (t *Token) Metadata() map[string]string {
return t.meta
}
+func (t *Token) GetMetadata(key string) (string, bool) {
+ val, ok := t.meta[key]
+ return val, ok
+}
+
+func (t *Token) SetMetadata(key string, value string) {
+ if t.meta == nil {
+ t.meta = make(map[string]string)
+ }
+ t.meta[key] = value
+}
+
func (t *Token) toConfig() map[string]string {
return map[string]string{
tokenValueKey: t.Value,
diff --git a/bridge/core/config.go b/bridge/core/config.go
index 9a8bc959..adee5f08 100644
--- a/bridge/core/config.go
+++ b/bridge/core/config.go
@@ -1 +1,43 @@
package core
+
+import (
+ "github.com/MichaelMure/git-bug/cache"
+ "github.com/MichaelMure/git-bug/identity"
+)
+
+func FinishConfig(repo *cache.RepoCache, metaKey string, login string) error {
+ // if no user exist with the given login metadata
+ _, err := repo.ResolveIdentityImmutableMetadata(metaKey, login)
+ if err != nil && err != identity.ErrIdentityNotExist {
+ // real error
+ return err
+ }
+ if err == nil {
+ // found an already valid user, all good
+ return nil
+ }
+
+ // if a default user exist, tag it with the login
+ user, err := repo.GetUserIdentity()
+ if err != nil && err != identity.ErrIdentityNotExist {
+ // real error
+ return err
+ }
+ if err == nil {
+ // found one
+ user.SetMetadata(metaKey, login)
+ return user.CommitAsNeeded()
+ }
+
+ // otherwise create a user with that metadata
+ i, err := repo.NewIdentityFromGitUserRaw(map[string]string{
+ metaKey: login,
+ })
+
+ err = repo.SetUserIdentity(i)
+ if err != nil {
+ return err
+ }
+
+ return nil
+}