refactor authentication

This commit is contained in:
DBras 2025-03-02 15:31:45 +01:00
parent 2ec27d11ab
commit b0400b7f95
2 changed files with 67 additions and 54 deletions

61
authentication.go Normal file
View File

@ -0,0 +1,61 @@
package main
import (
"context"
"fmt"
"log"
"os"
"encoding/json"
"net/http"
"path/filepath"
"golang.org/x/oauth2"
)
func getClient(config *oauth2.Config) *http.Client {
token_file := filepath.Join(CREDENTIALS_DIR, TOKEN_FILE)
token, err := tokenFromFile(token_file)
if err != nil {
token = getTokenFromWeb(config)
saveToken(token_file, token)
}
return config.Client(context.Background(), token)
}
func getTokenFromWeb(config *oauth2.Config) *oauth2.Token {
auth_url := config.AuthCodeURL("state-token", oauth2.AccessTypeOffline)
fmt.Printf("Go to the following link in your browser, then type the " +
"authorization code: \n%v\n", auth_url)
var auth_code string
if _, err := fmt.Scan(&auth_code) ; err != nil {
log.Fatalf("Unable to read authorization code: %v", err)
}
token, err := config.Exchange(context.TODO(), auth_code)
if err != nil {
log.Fatalf("Unable to retrieve token from web: %v", err)
}
return token
}
func tokenFromFile(file string) (*oauth2.Token, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
token := &oauth2.Token{}
err = json.NewDecoder(f).Decode(token)
return token, err
}
func saveToken(path string, token *oauth2.Token) {
fmt.Printf("Saving credential file to: %s\n", path)
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
log.Fatalf("Unable to cache oauth token: %v", err)
}
defer file.Close()
json.NewEncoder(file).Encode(token)
}

60
main.go
View File

@ -2,72 +2,24 @@ package main
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"log" "log"
"net/http"
"os" "os"
"path/filepath"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google" "golang.org/x/oauth2/google"
"google.golang.org/api/gmail/v1" "google.golang.org/api/gmail/v1"
"google.golang.org/api/option" "google.golang.org/api/option"
) )
const TOKEN_FILE string = "creds/token.json" const CREDENTIALS_DIR = "creds"
const CREDENTIALS_FILE string = "creds/credentials.json" const TOKEN_FILE = "token.json"
const CREDENTIALS_FILE = "credentials.json"
func getClient(config *oauth2.Config) *http.Client {
token_file := TOKEN_FILE
token, err := tokenFromFile(token_file)
if err != nil {
token = getTokenFromWeb(config)
saveToken(token_file, token)
}
return config.Client(context.Background(), token)
}
func getTokenFromWeb(config *oauth2.Config) *oauth2.Token {
auth_url := config.AuthCodeURL("state-token", oauth2.AccessTypeOffline)
fmt.Printf("Go to the following link in your browser, then type the " +
"authorization code: \n%v\n", auth_url)
var auth_code string
if _, err := fmt.Scan(&auth_code) ; err != nil {
log.Fatalf("Unable to read authorization code: %v", err)
}
token, err := config.Exchange(context.TODO(), auth_code)
if err != nil {
log.Fatalf("Unable to retrieve token from web: %v", err)
}
return token
}
func tokenFromFile(file string) (*oauth2.Token, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
token := &oauth2.Token{}
err = json.NewDecoder(f).Decode(token)
return token, err
}
func saveToken(path string, token *oauth2.Token) {
fmt.Printf("Saving credential file to: %s\n", path)
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
log.Fatalf("Unable to cache oauth token: %v", err)
}
defer file.Close()
json.NewEncoder(file).Encode(token)
}
func main() { func main() {
ctx := context.Background() ctx := context.Background()
creds_b, err := os.ReadFile(CREDENTIALS_FILE) creds_file := filepath.Join(CREDENTIALS_DIR, CREDENTIALS_FILE)
creds_b, err := os.ReadFile(creds_file)
if err != nil { if err != nil {
log.Fatalf("Unable to read client secret file: %v", err) log.Fatalf("Unable to read client secret file: %v", err)
} }