aboutsummaryrefslogtreecommitdiff
path: root/auth/token.go
diff options
context:
space:
mode:
authorVidhu Kant Sharma <vidhukant@vidhukant.xyz>2022-06-15 16:32:32 +0530
committerVidhu Kant Sharma <vidhukant@vidhukant.xyz>2022-06-15 16:32:32 +0530
commit1d72b87bca4042d36cea1f1e775803a5252cd224 (patch)
tree04a0185a473c7f9acd5c92bc4091d7c66f102e6b /auth/token.go
parent83e1ce2508ea035fb299a88fe89e82f34e609499 (diff)
now macli handles logging in by itself
Diffstat (limited to 'auth/token.go')
-rw-r--r--auth/token.go70
1 files changed, 68 insertions, 2 deletions
diff --git a/auth/token.go b/auth/token.go
index 5997025..fb1073f 100644
--- a/auth/token.go
+++ b/auth/token.go
@@ -24,6 +24,9 @@ import (
"github.com/zalando/go-keyring"
)
+var refreshPrefix string = "-refresh-token"
+var expiresPrefix string = "-expires-in"
+
func GetToken() string {
secret, err := keyring.Get(serviceName, userName)
if err != nil {
@@ -45,9 +48,72 @@ func setToken(secret string) {
func deleteToken() {
err := keyring.Delete(serviceName, userName)
- // TODO: if secret doesnt exist dont show error
+ // if secret doesnt exist dont show error
+ if err != nil {
+ if err.Error() != "secret not found in keyring" {
+ fmt.Println("Error while deleting authentication token", err.Error())
+ os.Exit(1)
+ }
+ }
+}
+
+// currently refreshtoken has no use
+func setRefreshToken(secret string) {
+ err := keyring.Set(serviceName + refreshPrefix, userName, secret)
+ if err != nil {
+ fmt.Println("Error while writing access token to keychain", err)
+ os.Exit(1)
+ }
+}
+
+func getRefreshToken() string {
+ secret, err := keyring.Get(serviceName + refreshPrefix, userName)
+ if err != nil {
+ fmt.Println("\x1b[31mError while reading refresh token from keychain:", err.Error(), "\x1b[0m")
+ fmt.Println("Your access token won't be automatically refreshed. If you have problems using macli please run `macli login` to log in again.")
+ os.Exit(1)
+ }
+
+ return secret
+}
+
+func deleteRefreshToken() {
+ err := keyring.Delete(serviceName + refreshPrefix, userName)
+ // if secret doesnt exist dont show error
+ if err != nil {
+ if err.Error() != "secret not found in keyring" {
+ fmt.Println("Error while deleting refresh token", err.Error())
+ os.Exit(1)
+ }
+ }
+}
+
+func setExpiresIn(secret string) {
+ err := keyring.Set(serviceName + expiresPrefix, userName, secret)
if err != nil {
- fmt.Println(err)
+ fmt.Println("Error while writing token expire time to keychain", err)
os.Exit(1)
}
}
+
+func getExpiresIn() string {
+ secret, err := keyring.Get(serviceName + expiresPrefix, userName)
+ if err != nil {
+ fmt.Println("\x1b[31mError while reading token expire time from keychain:", err.Error(), "\x1b[0m")
+ fmt.Println("Please log in again using `macli login` if problems occour")
+ // os.Exit(1)
+ }
+
+ return secret
+}
+
+func deleteExpiresIn() {
+ err := keyring.Delete(serviceName + expiresPrefix, userName)
+ // if secret doesnt exist dont show error
+ if err != nil {
+ if err.Error() != "secret not found in keyring" {
+ fmt.Println("Error while deleting token expires in data", err.Error())
+ os.Exit(1)
+ }
+ }
+}