Read tokens from store when server starts
Attempting to read the persisted tokens from the key-value store when the server begins. The server currently fails when those values are empty. TODO - Consider adding logic for knowing if the cached tokens are expired and prompt the user to reauthorize the client using a web browser.
This commit is contained in:
parent
248c7a24c6
commit
0f914d8aa0
1 changed files with 14 additions and 3 deletions
|
@ -16,6 +16,7 @@ import (
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
"kv"
|
||||||
)
|
)
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -70,8 +71,6 @@ var chans = &channels{
|
||||||
var (
|
var (
|
||||||
monzoClientId = os.Getenv("monzo_client_id")
|
monzoClientId = os.Getenv("monzo_client_id")
|
||||||
monzoClientSecret = os.Getenv("monzo_client_secret")
|
monzoClientSecret = os.Getenv("monzo_client_secret")
|
||||||
cachedAccessToken = os.Getenv("monzo_cached_access_token")
|
|
||||||
cachedRefreshToken = os.Getenv("monzo_cached_access_token")
|
|
||||||
)
|
)
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -124,9 +123,21 @@ func refreshTokens(refreshToken string) (string, string) {
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
// Retrieve cached tokens from store.
|
||||||
|
accessToken := fmt.Sprintf("%v", kv.Get("monzoAccessToken"))
|
||||||
|
refreshToken := fmt.Sprintf("%v", kv.Get("monzoRefreshToken"))
|
||||||
|
|
||||||
|
log.Println("Attempting to retrieve cached credentials...")
|
||||||
|
log.Printf("Access token: %s\n", accessToken)
|
||||||
|
log.Printf("Refresh token: %s\n", refreshToken)
|
||||||
|
|
||||||
|
if accessToken == "" || refreshToken == "" {
|
||||||
|
log.Fatal("Cannot start server without access or refresh tokens.")
|
||||||
|
}
|
||||||
|
|
||||||
// Manage application state.
|
// Manage application state.
|
||||||
go func() {
|
go func() {
|
||||||
state := &state{cachedAccessToken, cachedRefreshToken}
|
state := &state{accessToken, refreshToken}
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case msg := <-chans.reads:
|
case msg := <-chans.reads:
|
||||||
|
|
Loading…
Reference in a new issue