implentented token and added comments

This commit is contained in:
Suguivy 2022-11-30 17:49:57 +01:00
parent c98343222d
commit 823fa374dc
1 changed files with 72 additions and 31 deletions

103
main.go
View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"bufio"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
@ -10,60 +11,100 @@ import (
"os" "os"
) )
type app struct {
ClientId string `json:"client_id"`
ClientSecret string `json:"client_secret"`
}
type user struct {
Username string `json:"username"`
Instance string `json:"instance"`
AccessToken string `json:"access_token"`
}
var BASE_URL = "https://awoo.fai.st" var BASE_URL = "https://awoo.fai.st"
var BASE_API_URL = "https://awoo.fai.st/api/v1" var DEFAULT_REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob"
func main() { func main() {
client_id, client_secret, err := get_app_credentials() // Get the `client_id` and the `client_secret` values
app, err := getAppCredentials(BASE_URL)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
fmt.Println(client_id, client_secret) fmt.Println(getTokenUrl(BASE_URL, app))
// Read the token
fmt.Print("Enter token: ")
reader := bufio.NewReader(os.Stdin)
token, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err)
}
token = token[:len(token)-1]
fmt.Println(token)
} }
func get_app_credentials() (string, string, error) { func getTokenUrl(instance string, app *app) string {
return fmt.Sprintf("%s/oauth/authorize?response_type=code&client_id=%s&redirect_uri=%s", BASE_URL, app.ClientId, DEFAULT_REDIRECT_URI)
}
func getAppCredentials(instance string) (*app, error) {
// We try to read the file `credentials.json` to read
// the application's credentials
// If it doesn't exist, we generate a new application
contents, err := os.ReadFile("credentials.json") contents, err := os.ReadFile("credentials.json")
if err != nil { if err != nil {
return create_app() return createApp(instance)
} }
var json_data map[string]string
err = json.Unmarshal(contents, &json_data)
if err != nil {
return "", "", err
}
client_id := json_data["client_id"]
client_secret := json_data["client_secret"]
return client_id, client_secret, nil // The file's contents are unmarshalled into a map
var credentials app
err = json.Unmarshal(contents, &credentials)
if err != nil {
return nil, err
}
return &credentials, nil
} }
func create_app() (string, string, error) { func createApp(instance string) (*app, error) {
// Define the values to the request for creating the app
data := url.Values{ data := url.Values{
"client_name": {"grames"}, "client_name": {"grames"},
"redirect_uris": {"urn:ietf:wg:oauth:2.0:oob"}, "redirect_uris": {DEFAULT_REDIRECT_URI},
} }
resp, err := http.PostForm(BASE_API_URL+"/apps", data) // Send request to create app
resp, err := http.PostForm(instance+"/api/v1/apps", data)
if err != nil { if err != nil {
return "", "", err return nil, err
} }
defer resp.Body.Close() defer resp.Body.Close()
// Read the body's JSON response into `contents` as []byte
contents, err := io.ReadAll(resp.Body) contents, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return "", "", err return nil, err
} }
err = os.WriteFile("credentials.json", contents, 0644)
if err != nil {
return "", "", err
}
var json_data map[string]string
err = json.Unmarshal(contents, &json_data)
if err != nil {
return "", "", err
}
client_id := json_data["client_id"]
client_secret := json_data["client_secret"]
return client_id, client_secret, nil // Unmarshal the JSON data into an app struct
var credentials app
err = json.Unmarshal(contents, &credentials)
if err != nil {
return nil, err
}
// Marshal the app into JSON data
b, err := json.MarshalIndent(credentials, "", " ")
if err != nil {
return nil, err
}
// Write the JSON data into the file `credentials.json`
err = os.WriteFile("credentials.json", b, 0644)
if err != nil {
return nil, err
}
return &credentials, nil
} }