oauth can refresh

This commit is contained in:
bel
2026-02-23 23:58:23 -07:00
parent 5a4ce70451
commit 1b74eb0d16

View File

@@ -1,18 +1,22 @@
package contact package contact
import ( import (
"bytes"
"context" "context"
"crypto/sha256" "crypto/sha256"
"crypto/tls" "crypto/tls"
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"log" "log"
"net/http"
"net/mail" "net/mail"
"net/smtp" "net/smtp"
"net/url" "net/url"
"os" "os"
"strings" "strings"
"time"
"github.com/bytbox/go-pop3" "github.com/bytbox/go-pop3"
"github.com/emersion/go-imap" "github.com/emersion/go-imap"
@@ -97,12 +101,44 @@ func (e *Emailer) oauthThenReadIMAP() (chan *mail.Message, error) {
os.WriteFile(e.OAuth+".token", tokenFileJSON, os.ModePerm) os.WriteFile(e.OAuth+".token", tokenFileJSON, os.ModePerm)
log.Printf("%s", tokenFileJSON) log.Printf("%s", tokenFileJSON)
} }
token, _ := os.ReadFile(e.OAuth + ".token")
var tokenStruct struct { var tokenStruct struct {
AccessToken string `json:"access_token"` AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
Expiry time.Time `json:"expiry"`
ExpiresIn int `json:"expires_in"`
} }
token, _ := os.ReadFile(e.OAuth + ".token")
json.Unmarshal(token, &tokenStruct) json.Unmarshal(token, &tokenStruct)
log.Println(e.From, tokenStruct.AccessToken) if time.Until(tokenStruct.Expiry) < time.Minute*5 {
var appStruct struct {
Installed struct {
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
GrantType string `json:"grant_type"`
RefreshToken string `json:"refresh_token"`
} `json:"installed"`
}
json.Unmarshal([]byte(configJSON), &appStruct)
appStruct.Installed.GrantType = "refresh_token"
appStruct.Installed.RefreshToken = tokenStruct.RefreshToken
b, _ := json.Marshal(appStruct.Installed)
resp, err := http.Post(`https://oauth2.googleapis.com/token`, `application/json`, bytes.NewReader(b))
if err != nil {
return nil, err
}
defer resp.Body.Close()
b, _ = io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("(%d) %s", resp.StatusCode, b)
}
tokenStruct.Expiry = time.Time{}
json.Unmarshal(b, &tokenStruct)
tokenStruct.Expiry = time.Now().Add(time.Duration(tokenStruct.ExpiresIn) * time.Second)
tokenStruct.RefreshToken = appStruct.Installed.RefreshToken
b, _ = json.Marshal(tokenStruct)
os.WriteFile(e.OAuth+".token", b, os.ModePerm)
}
log.Println("OAUTH", e.From, time.Until(tokenStruct.Expiry), tokenStruct.AccessToken)
return e.readIMAP(e.From, tokenStruct.AccessToken) return e.readIMAP(e.From, tokenStruct.AccessToken)
} }