-
Notifications
You must be signed in to change notification settings - Fork 61
/
main.go
124 lines (92 loc) · 3.59 KB
/
main.go
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"crypto/ed25519"
"flag"
"fmt"
"log"
"os"
"github.com/spaceandtimelabs/SxT-Go-SDK/authentication"
"github.com/spaceandtimelabs/SxT-Go-SDK/helpers"
"github.com/spaceandtimelabs/SxT-Go-SDK/storage"
"github.com/spaceandtimelabs/SxT-Go-SDK/utils"
)
// Check the command line arguments
func isFlagPassed(name string) int {
count := 0
flag.Visit(func(f *flag.Flag) {
if f.Name == name {
count = 1
}
})
return count
}
// Main function
func main() {
fmt.Println("")
fmt.Println("For exisiting users")
fmt.Println("Usage: go run main.go -userid=<USERID> -pubkey=<BASE64 STD ENCODED PUBLIC KEY> -privkey=<BASE64 STD ENCODED PRIVATE KEY>")
fmt.Println("")
var privateKey ed25519.PrivateKey
var publicKey ed25519.PublicKey
var accessToken string
inputUserid := flag.String("userid", "", "(Optional) SxT userid. But if provided, the remaining values are required")
inputPubKey := flag.String("pubkey", "", "(Optional) Standard base64 encoded public key. But if provided, the remaining values are required")
inputPrivKey := flag.String("privkey", "", "(Optional) Standard base64 encoded private key. But if provided, the remaining values are required")
flag.Parse()
/*************************************
// Authentication Block
*************************************/
/* AUTH BLOCK STARTS */
totalFlags := isFlagPassed("userid") + isFlagPassed("pubkey") + isFlagPassed("privkey")
if totalFlags < 3 && totalFlags > 0 {
fmt.Println("=== Missing input values. Stopping program ===")
return
}
if isFlagPassed("userid")+isFlagPassed("pubkey")+isFlagPassed("privkey") == 3 {
if len(*inputUserid) > 0 || len(*inputPubKey) > 0 || len(*inputPrivKey) > 0 {
accessToken, _, privateKey, publicKey, _ = utils.Authenticate(*inputUserid, *inputPubKey, *inputPrivKey)
fmt.Println("=== Existing Login from user input ===", accessToken)
} else {
fmt.Println("=== Empty input values. Stopping program ===")
return
}
} else {
userId, _ := helpers.ReadUserId()
sessionData, status := storage.FileReadSession(userId)
if !status {
emptyString := ""
accessToken, _, privateKey, publicKey, _ = utils.Authenticate(emptyString, emptyString, emptyString)
fmt.Println("=== New Login. Creating new session ===")
} else {
validateTokenStatus := authentication.ValidateToken(sessionData.AccessToken)
if !validateTokenStatus {
refreshTokenStruct, refreshTokenStatus := authentication.RefreshToken(sessionData.RefreshToken)
if refreshTokenStatus {
fmt.Println("=== Invalid session on session.txt file. Using refresh token ===")
privateKey = sessionData.PrivateKey
publicKey = sessionData.PublicKey
accessToken = refreshTokenStruct.AccessToken
writeStatus := storage.FileWriteSession(userId, refreshTokenStruct.AccessToken, refreshTokenStruct.RefreshToken, sessionData.PrivateKey, sessionData.PublicKey)
if !writeStatus {
log.Fatal("Invalid login. Change login credentials")
}
} else {
fmt.Println("=== Invalid session on session.txt file. Issuing new token ===")
emptyString := ""
accessToken, _, privateKey, publicKey, _ = utils.Authenticate(emptyString, emptyString, emptyString)
}
} else {
fmt.Println("=== Login using session.txt file ===")
privateKey = sessionData.PrivateKey
publicKey = sessionData.PublicKey
accessToken = sessionData.AccessToken
}
}
}
// Important : Set the accessToken in Environment variable to access in later
os.Setenv("accessToken", accessToken)
/* AUTH BLOCK ENDS */
// Call SQL functions
utils.SQLAPIs(privateKey, publicKey)
utils.DiscoveryAPIs()
}