summary refs log tree commit diff
path: root/authconf.go
blob: 2e9b81b07e3e39aa9d3485c0e1b09942e15f1e20 (plain)
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
package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type UTRSimple struct {
	AccessToken  string   `json:"access_token"`
	ExpiresIn    int      `json:"expires_in"`
	RefreshToken string   `json:"refresh_token"`
}

func Auth(authFile string) UTRSimple {
	_ , ferr := os.Stat(authFile)
	if ferr != nil {
		WriteAuth(authFile)
	}
	return ReadAuth(authFile)
}

func WriteAuth(authFile string) {
	authToken, err := GetOAuth(clientID)
	if err != nil {
		panic(err)
	}
	
	uT := GetUserToken(authToken)
	
	err = os.MkdirAll("/home/venomade/.local/share/lifesigns", 0755)
	// TODO: unhardcode
	
	if err != nil {
		panic(err)
	}

	file, err := os.Create(authFile)
	if err != nil {
		panic(err)
	}
	defer file.Close()

	confString := fmt.Sprintf("%s\n%s\n%s", uT.AccessToken, uT.ExpiresIn, uT.RefreshToken)
	_, err = file.WriteString(confString)
	if err != nil {
		panic(err)
	}
}

func ReadAuth(authFile string) UTRSimple{
	file, err := os.Open(authFile)
	if err != nil {
		fmt.Println("Error opening file:", err)
		panic(err)
	}
	defer file.Close()

	// Create a slice to hold the lines of the file
	var lines []string

	// Read all lines from the file into the slice
	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		lines = append(lines, scanner.Text())
	}

	var utrs UTRSimple
	
	// Process each line using the appropriate function
	if len(lines) >= 2 { // Change to 3
		utrs.AccessToken = lines[0]
		exp, _ := strconv.Atoi(lines[1])
		utrs.ExpiresIn = exp
		utrs.RefreshToken = lines[2]
	} else {
		fmt.Println("File does not contain enough lines.")
	}

	return utrs
}