forked from stapelberg/scan2drive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.go
137 lines (125 loc) · 3.5 KB
/
setup.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"sync"
"github.com/sdier/scan2drive/templates"
)
type setupMux struct {
setup bool
mu sync.RWMutex
cond *sync.Cond
setupMux *http.ServeMux
defaultMux *http.ServeMux
}
func newSetupMux() *setupMux {
m := &setupMux{
setupMux: http.NewServeMux(),
defaultMux: http.DefaultServeMux,
}
m.cond = sync.NewCond(&m.mu)
return m
}
func (s *setupMux) setSetup(setup bool) {
s.mu.Lock()
defer s.mu.Unlock()
s.setup = setup
s.cond.Broadcast()
}
// setupLeft blocks until setup mode is left
func (s *setupMux) setupLeft() {
s.cond.L.Lock()
for s.setup {
s.cond.Wait()
}
s.cond.L.Unlock()
}
// ServeHTTP implements (net/http).Handler
func (s *setupMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.mu.RLock()
mux := s.defaultMux
if s.setup {
mux = s.setupMux
}
s.mu.RUnlock()
mux.ServeHTTP(w, r)
}
func writeSecretFile(clientid, clientsecret string) error {
if clientid == "" || clientsecret == "" {
return fmt.Errorf("either clientid=%q or clientsecret=%q are empty", clientid, clientsecret)
}
// Use a temporary file to rule out clobbering *clientSecretPath,
// should it in the meantime have been created.
f, err := ioutil.TempFile(filepath.Dir(*clientSecretPath), "scan2drive")
if err != nil {
return err
}
defer f.Close()
type installed struct {
ClientId string `json:"client_id"`
ClientSecret string `json:"client_secret"`
AuthUri string `json:"auth_uri"`
TokenUri string `json:"token_uri"`
AuthProviderX509CertUrl string `json:"auth_provider_x509_cert_url"`
RedirectUris []string `json:"redirect_uris"`
}
if err := json.NewEncoder(f).Encode(&struct {
Installed installed `json:"installed"`
}{
Installed: installed{
ClientId: clientid,
ClientSecret: clientsecret,
AuthUri: "https://accounts.google.com/o/oauth2/auth",
TokenUri: "https://accounts.google.com/o/oauth2/token",
AuthProviderX509CertUrl: "https://www.googleapis.com/oauth2/v1/certs",
RedirectUris: []string{
"urn:ietf:wg:oauth:2.0:oob",
"postmessage",
},
},
}); err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
return os.Rename(f.Name(), *clientSecretPath)
}
func registerSetupHandlers(mux *setupMux) {
var canWriteClientSecret bool
if err := ioutil.WriteFile(*clientSecretPath, nil, 0600); err == nil {
canWriteClientSecret = true
os.Remove(*clientSecretPath)
} else {
log.Printf("Cannot offer a web form for storing the client secret: writing test file failed: %v", err)
}
mux.setupMux.HandleFunc("/assets/", assetsDirHandler)
mux.setupMux.HandleFunc("/setup", func(w http.ResponseWriter, r *http.Request) {
clientid := r.FormValue("clientid")
clientsecret := r.FormValue("clientsecret")
if err := writeSecretFile(clientid, clientsecret); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
mux.setSetup(false)
http.Redirect(w, r, "/", http.StatusFound)
})
mux.setupMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
var buf bytes.Buffer
if err := templates.SetupTpl.ExecuteTemplate(&buf, "Setup", map[string]interface{}{
"ClientSecretPath": *clientSecretPath,
"CanWriteClientSecret": canWriteClientSecret,
}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
io.Copy(w, &buf)
})
}