-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathlogin_validate.go
More file actions
51 lines (45 loc) · 1.81 KB
/
login_validate.go
File metadata and controls
51 lines (45 loc) · 1.81 KB
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
package main
import (
"context"
"fmt"
"io"
"net/url"
"strings"
"github.com/sourcegraph/src-cli/internal/api"
"github.com/sourcegraph/src-cli/internal/cmderrors"
)
func runMissingAuthLogin(_ context.Context, p loginParams) error {
fmt.Fprintln(p.out)
printLoginProblem(p.out, "No access token is configured.")
fmt.Fprintln(p.out, loginAccessTokenMessage(p.cfg.endpointURL))
return cmderrors.ExitCode1
}
func runValidatedLogin(ctx context.Context, p loginParams) error {
return validateCurrentUser(ctx, p.client, p.out, p.cfg.endpointURL)
}
func validateCurrentUser(ctx context.Context, client api.Client, out io.Writer, endpointURL *url.URL) error {
query := `query CurrentUser { currentUser { username } }`
var result struct {
CurrentUser *struct{ Username string }
}
if _, err := client.NewRequest(query, nil).Do(ctx, &result); err != nil {
if strings.HasPrefix(err.Error(), "error: 401 Unauthorized") || strings.HasPrefix(err.Error(), "error: 403 Forbidden") {
printLoginProblem(out, "Invalid access token.")
} else {
printLoginProblem(out, fmt.Sprintf("Error communicating with %s: %s", endpointURL, err))
}
fmt.Fprintln(out, loginAccessTokenMessage(endpointURL))
fmt.Fprintln(out, " (If you need to supply custom HTTP request headers, see information about SRC_HEADER_* and SRC_HEADERS env vars at https://github.com/sourcegraph/src-cli/blob/main/AUTH_PROXY.md)")
return cmderrors.ExitCode1
}
if result.CurrentUser == nil {
// This should never happen; we verified there is an access token, so there should always be
// a user.
printLoginProblem(out, fmt.Sprintf("Unable to determine user on %s.", endpointURL))
return cmderrors.ExitCode1
}
fmt.Fprintln(out)
fmt.Fprintf(out, "✔︎ Authenticated as %s on %s\n", result.CurrentUser.Username, endpointURL)
fmt.Fprintln(out)
return nil
}