forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken_tronity.go
139 lines (113 loc) Β· 2.81 KB
/
token_tronity.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
package cmd
import (
"context"
"crypto/rand"
"encoding/base64"
"errors"
"fmt"
"io"
"net/http"
"strings"
"sync"
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/vehicle"
"github.com/evcc-io/evcc/vehicle/tronity"
"github.com/skratchdot/open-golang/open"
"golang.org/x/oauth2"
)
// github.com/uhthomas/tesla
func state() string {
var b [9]byte
if _, err := io.ReadFull(rand.Reader, b[:]); err != nil {
panic(err)
}
return base64.RawURLEncoding.EncodeToString(b[:])
}
func tokenExchangeHandler(oc *oauth2.Config, state string, resC chan *oauth2.Token) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if remote := r.URL.Query().Get("state"); state != remote {
w.WriteHeader(http.StatusBadRequest)
resC <- nil
return
}
code := r.URL.Query().Get("code")
ctx := context.Background()
token, err := oc.Exchange(ctx, code,
oauth2.SetAuthURLParam("grant_type", "code"), // app
)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintln(w, err)
resC <- nil
return
}
fmt.Fprintln(w, "Token received, see console")
resC <- token
}
}
func tronityAuthorize(addr string, oc *oauth2.Config) (*oauth2.Token, error) {
state := state()
uri := oc.AuthCodeURL(state, oauth2.AccessTypeOffline)
uri = strings.ReplaceAll(uri, "scope=", "scopes=")
if err := open.Start(uri); err != nil {
return nil, err
}
resC := make(chan *oauth2.Token)
handler := tokenExchangeHandler(oc, state, resC)
defer close(resC)
// handle request
mux := &http.ServeMux{}
mux.HandleFunc("/auth/tronity", handler)
wg := new(sync.WaitGroup)
s := &http.Server{
Addr: addr,
Handler: mux,
}
// start server
wg.Add(1)
go func() {
if err := s.ListenAndServe(); err != http.ErrServerClosed {
log.FATAL.Fatal(err)
}
wg.Done()
}()
// close on exit
defer func() {
_ = s.Close()
wg.Wait()
close(resC)
}()
t := time.NewTimer(time.Minute)
select {
case <-t.C:
return nil, api.ErrTimeout
case token := <-resC:
if token == nil {
return nil, errors.New("token not received")
}
return token, nil
}
}
func tronityToken(conf config, vehicleConf qualifiedConfig) (*oauth2.Token, error) {
cc := struct {
Credentials vehicle.ClientCredentials
RedirectURI string
Other map[string]interface{} `mapstructure:",remain"`
}{}
if err := util.DecodeOther(vehicleConf.Other, &cc); err != nil {
return nil, err
}
if err := cc.Credentials.Error(); err != nil {
return nil, err
}
oc, err := tronity.OAuth2Config(cc.Credentials.ID, cc.Credentials.Secret)
if err != nil {
return nil, err
}
if oc.RedirectURL = cc.RedirectURI; oc.RedirectURL == "" {
oc.RedirectURL = fmt.Sprintf("%s/auth/tronity", conf.Network.URI())
}
return tronityAuthorize(conf.Network.HostPort(), oc)
}