-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.go
More file actions
122 lines (100 loc) · 3.04 KB
/
auth.go
File metadata and controls
122 lines (100 loc) · 3.04 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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"golang.org/x/oauth2"
)
var oauth2config oauth2.Config
func loadTokenFile() oauth2.Token {
var token oauth2.Token
file, err := os.Open("token.json")
if err != nil {
log.Fatalln("Error - unable to open token file at token.json. You may need to login first. Here's the erro: ", err)
}
decoder := json.NewDecoder(file)
err = decoder.Decode(&token)
if err != nil {
log.Fatalln("Error - unable to decode token JSON file: ", err)
}
return token
}
// Homepage
func homePage(w http.ResponseWriter, r *http.Request) {
fmt.Println("Homepage hit")
u := oauth2config.AuthCodeURL(os.Getenv("STATE_CHECK"))
fmt.Println("URL: ", u)
http.Redirect(w, r, u, http.StatusFound)
}
// Authorize
func authorize(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
state := r.Form.Get("state")
if state != os.Getenv("STATE_CHECK") {
http.Error(w, "State invalid", http.StatusBadRequest)
return
}
code := r.Form.Get("code")
if code == "" {
http.Error(w, "Code not found", http.StatusBadRequest)
return
}
token, err := oauth2config.Exchange(context.Background(), code)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// print token to screen
e := json.NewEncoder(w)
e.SetIndent("", " ")
e.Encode(*token)
// print token to commandline
fmt.Println("Token: " + token.AccessToken)
//save token to file
file, err := json.MarshalIndent(token, "", " ")
if err != nil {
fmt.Printf("Error converting token to file: %s", err)
}
err = ioutil.WriteFile("token.json", file, 0644)
if err != nil {
fmt.Printf("Error saving token to token.json file: %s", err)
}
fmt.Println("Token successfully saved to token.json")
fmt.Println("Remember to save your VRN to the env.json file")
}
func auth() {
port := "3000"
host := "http://localhost:" + port
redirectEndPoint := "/oauth2"
fmt.Println(host + redirectEndPoint)
oauth2config = oauth2.Config{
ClientID: os.Getenv("CLIENT_ID"),
ClientSecret: os.Getenv("CLIENT_SECRET"),
Scopes: []string{"read:vat", "write:vat"}, //write:vat
// Scopes: []string{"Read-Only"},
RedirectURL: host + redirectEndPoint,
// This points to our Authorization Server
// if our Client ID and Client Secret are valid
// it will attempt to authorize our user
Endpoint: oauth2.Endpoint{
AuthURL: os.Getenv("API_URL") + "/oauth/authorize",
TokenURL: os.Getenv("API_URL") + "/oauth/token",
},
}
// 1 - We attempt to hit our Homepage route
// if we attempt to hit this unauthenticated, it
// will automatically redirect to our Auth
// server and prompt for login credentials
http.HandleFunc("/", homePage)
// 2 - This displays our state, code and
// token and expiry time that we get back
// from our Authorization server
http.HandleFunc(redirectEndPoint, authorize)
// 3 - We start up our Client on port 3000
log.Printf("Client is running at %s port. Open %s in your browser to authenticate this device.\n", port, host)
log.Fatal(http.ListenAndServe(":"+port, nil))
}