-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
88 lines (69 loc) · 1.97 KB
/
main.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"context"
"fmt"
"log"
"os"
"time"
phabricator "github.com./Megaputer/go_oauth_phabricator/v2"
)
// initialize the client.
func initialize() (*phabricator.Config, error) {
// Get phid and secret from
// https://phabricator.example.net/oauthserver/query/all/
phid := "PHID-OASC-..."
secret := "Application Secret"
// redirectURL is the URL to redirect users going through
// the OAuth flow, after the resource owner's URLs
redirectURL := "https://my.net/auth"
// phabricatorURL the url of the phabricator server
// that is the source of OAuth
phabricatorURL := "https://phabricator.example.net/"
client, err := phabricator.New(phid, secret, redirectURL, phabricatorURL)
if err != nil {
return nil, fmt.Errorf("client: %w", err)
}
return client, nil
}
func main() {
ctx := context.Background()
err := run(ctx)
if err != nil {
log.Fatal(err)
}
}
func run(ctx context.Context) error {
ctx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
client, err := initialize()
if err != nil {
return fmt.Errorf("initialize: %w", err)
}
// AuthCodeURL return url from OAuth with CSRF token
url := client.AuthCodeURL("csrf_token")
log.Printf("Open link: %s", url)
// code will be in the *http.Request.FormValue("code")
// https://secure.phabricator.com/book/phabcontrib/article/using_oauthserver/
code := "code_data"
const (
countArg = 2
)
if len(os.Args) != countArg {
log.Print("Usage: \n\t$ go run cmd/example/main.go 'code_value'")
} else {
code = os.Args[1]
}
user, err := client.Authenticate(ctx, code)
if err != nil {
return fmt.Errorf("client.Authenticate: %w", err)
}
log.Print("User data:")
log.Printf("\tPHID: '%s'", user.PHID)
log.Printf("\tUserName: '%s'", user.UserName)
log.Printf("\tRealName: '%s'", user.RealName)
log.Printf("\tImage: '%s'", user.Image)
log.Printf("\tURI: '%s'", user.URI)
log.Printf("\tRoles: '%s'", user.Roles)
log.Printf("\tPrimaryEmail: '%s'", user.PrimaryEmail)
return nil
}