diff options
Diffstat (limited to 'authconf.go')
-rw-r--r-- | authconf.go | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/authconf.go b/authconf.go new file mode 100644 index 0000000..2e9b81b --- /dev/null +++ b/authconf.go @@ -0,0 +1,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 +} |