aboutsummaryrefslogtreecommitdiffstats
path: root/bridge/core/auth/login.go
blob: 496f2412fa666194cb307fb7557b854353e966ce (plain) (blame)
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
package auth

import (
	"crypto/sha256"
	"fmt"

	"github.com/MichaelMure/git-bug/entity"
)

const (
	keyringKeyLoginLogin = "login"
)

var _ Credential = &Login{}

type Login struct {
	*credentialBase
	Login string
}

func NewLogin(target, login string) *Login {
	return &Login{
		credentialBase: newCredentialBase(target),
		Login:          login,
	}
}

func NewLoginFromConfig(conf map[string]string) (*Login, error) {
	base, err := newCredentialBaseFromData(conf)
	if err != nil {
		return nil, err
	}

	return &Login{
		credentialBase: base,
		Login:          conf[keyringKeyLoginLogin],
	}, nil
}

func (lp *Login) ID() entity.Id {
	h := sha256.New()
	_, _ = h.Write(lp.salt)
	_, _ = h.Write([]byte(lp.target))
	_, _ = h.Write([]byte(lp.Login))
	return entity.Id(fmt.Sprintf("%x", h.Sum(nil)))
}

func (lp *Login) Kind() CredentialKind {
	return KindLogin
}

func (lp *Login) Validate() error {
	err := lp.credentialBase.validate()
	if err != nil {
		return err
	}
	if lp.Login == "" {
		return fmt.Errorf("missing login")
	}
	return nil
}

func (lp *Login) toConfig() map[string]string {
	return map[string]string{
		keyringKeyLoginLogin: lp.Login,
	}
}