-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
197 lines (177 loc) · 5.94 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"os"
"sync"
"github.com/alecthomas/kong"
goshopify "github.com/bold-commerce/go-shopify/v3"
)
var version string = "v0.0.0-unset"
type config struct {
Address string `help:"" default:":8080"`
BaseURL url.URL `help:"base URL used in redirect"`
}
type server struct {
locker *sync.RWMutex
apps map[string]goshopify.App
mux *http.ServeMux
redirectURL *url.URL
authURL *url.URL
}
type App struct {
Name string
ClientID string
ClientSecret string
Scope string
}
func newServer(cfg config) *server {
redirectPath := "/redirect"
authPath := "/auth"
s := &server{
locker: &sync.RWMutex{},
apps: map[string]goshopify.App{},
mux: http.NewServeMux(),
redirectURL: cfg.BaseURL.JoinPath(redirectPath),
authURL: cfg.BaseURL.JoinPath(authPath),
}
s.mux.HandleFunc(authPath, s.handleAuth)
s.mux.HandleFunc(redirectPath, s.handleRedirect)
s.mux.HandleFunc("/new", s.handleNew)
s.mux.HandleFunc("/version", s.version)
s.mux.HandleFunc("/", s.handleNoMatch)
return s
}
func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.mux.ServeHTTP(w, r)
}
func (s *server) setApp(appName string, app goshopify.App) {
s.locker.Lock()
defer s.locker.Unlock()
s.apps[appName] = app
}
func (s *server) app(appName string) (goshopify.App, bool) {
s.locker.RLock()
defer s.locker.RUnlock()
app, ok := s.apps[appName]
return app, ok
}
func (s *server) makeRedirectURL(appName string) string {
u := *s.redirectURL
q := u.Query()
q.Set("app", appName)
u.RawQuery = q.Encode()
return u.String()
}
// handleAuth redirects to an oauth-authorize url for the app.
func (s *server) handleAuth(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(os.Stdout, "auth: url: %s method: %s\n", r.URL.String(), r.Method)
if r.Method != http.MethodGet {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
appName := r.URL.Query().Get("app")
app, ok := s.app(appName)
if !ok {
msg := fmt.Sprintf("app %q not found. create new app at '/new'", appName)
http.Error(w, msg, http.StatusBadRequest)
return
}
shopName := r.URL.Query().Get("shop")
state := "nonce"
authUrl := app.AuthorizeUrl(shopName, state)
fmt.Println()
http.Redirect(w, r, authUrl, http.StatusFound)
}
// handleRedirect fetches a permanent access token in the callback
func (s *server) handleRedirect(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(os.Stdout, "redirect: url: %s method: %s\n", r.URL.String(), r.Method)
if r.Method != http.MethodGet {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
appName := r.URL.Query().Get("app")
app, ok := s.app(appName)
if !ok {
msg := fmt.Sprintf("app %q not found. create new app at '/new'", appName)
http.Error(w, msg, http.StatusBadRequest)
return
}
if ok, _ := app.VerifyAuthorizationURL(r.URL); !ok {
http.Error(w, "Invalid Signature", http.StatusUnauthorized)
return
}
query := r.URL.Query()
shopName := query.Get("shop")
code := query.Get("code")
token, err := app.GetAccessToken(shopName, code)
if err != nil {
log.Println("cannot get access token", err)
return
}
fmt.Println("token", token)
fmt.Fprintf(w, "%q connected successfully.\n", appName)
// Do something with the token, like store it in a DB.
}
// handleRedirect fetches a permanent access token in the callback
func (s *server) handleNew(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(os.Stdout, "new: url: %s method: %s\n", r.URL.String(), r.Method)
if r.Method != http.MethodPost {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
app := App{}
err := json.NewDecoder(r.Body).Decode(&app)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if app.Name == "" || app.ClientID == "" || app.ClientSecret == "" || app.Scope == "" {
msg := `"name", "clientID", "clientSecret" and "scope" cannot be empty`
http.Error(w, msg, http.StatusBadRequest)
return
}
shopifyApp := goshopify.App{
ApiKey: app.ClientID,
ApiSecret: app.ClientSecret,
Scope: app.Scope,
RedirectUrl: s.makeRedirectURL(app.Name),
}
s.setApp(app.Name, shopifyApp)
rawQuery := "app=" + url.QueryEscape(app.Name)
authURL := *s.authURL // copy
authURL.RawQuery = rawQuery
redirectURL := *s.redirectURL //copy
redirectURL.RawQuery = rawQuery
fmt.Println("successfully added", app.Name)
fmt.Fprintln(w, "successfully added", app.Name)
fmt.Fprintln(w, "auth url:", authURL.String())
fmt.Fprintln(w, "redirect url:", redirectURL.String())
}
func (s *server) version(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "version", version)
}
func (s *server) handleNoMatch(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(os.Stdout, "no-math: url: %s method: %s\n", r.URL.String(), r.Method)
msg := `valid end points are "/new", "/auth" and "/redirect"
Check the docs at: https://github.com/juliaogris/shopify`
http.Error(w, msg, http.StatusNotFound)
}
func main() {
opts := []kong.Option{
kong.Description("Basic Shopify Oauth demo server"),
kong.DefaultEnvars("shopify"),
}
cfg := config{}
_ = kong.Parse(&cfg, opts...)
fmt.Printf("%#v\n", cfg)
server := newServer(cfg)
fmt.Println("starting server on http://localhost:8080")
if err := http.ListenAndServe(cfg.Address, server); err != nil {
fmt.Println(err)
os.Exit(1)
}
}