-
Notifications
You must be signed in to change notification settings - Fork 8
/
auth_login.go
55 lines (45 loc) · 1.34 KB
/
auth_login.go
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
package main
import (
"context"
"fmt"
"github.com/charmbracelet/log"
"go.abhg.dev/gs/internal/forge"
"go.abhg.dev/gs/internal/secret"
"go.abhg.dev/gs/internal/text"
)
type authLoginCmd struct {
Refresh bool `help:"Force a refresh of the authentication token"`
}
func (*authLoginCmd) Help() string {
return text.Dedent(`
For GitHub, a prompt will allow selecting between
OAuth, GitHub App, and Personal Access Token-based authentication.
The differences between them are explained in the prompt.
The authentication token is stored in a system-provided secure storage.
Use 'gs auth logout' to log out and delete the token from storage.
Fails if already logged in.
Use --refresh to force a refresh of the authentication token,
or change the authentication method.
`)
}
func (cmd *authLoginCmd) Run(
ctx context.Context,
stash secret.Stash,
log *log.Logger,
globals *globalOptions,
f forge.Forge,
) error {
if _, err := f.LoadAuthenticationToken(stash); err == nil && !cmd.Refresh {
log.Errorf("Use --refresh to force a refresh of the authentication token")
return fmt.Errorf("%s: already logged in", f.ID())
}
secret, err := f.AuthenticationFlow(ctx)
if err != nil {
return err
}
if err := f.SaveAuthenticationToken(stash, secret); err != nil {
return err
}
log.Infof("%s: successfully logged in", f.ID())
return nil
}