grames/main.go

111 lines
2.4 KiB
Go
Raw Permalink Normal View History

2022-11-29 21:25:08 +00:00
package main
import (
2022-11-30 16:49:57 +00:00
"bufio"
2022-11-29 21:25:08 +00:00
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
)
2022-11-30 16:49:57 +00:00
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"`
}
2022-11-29 21:25:08 +00:00
var BASE_URL = "https://awoo.fai.st"
2022-11-30 16:49:57 +00:00
var DEFAULT_REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob"
2022-11-29 21:25:08 +00:00
func main() {
2022-11-30 16:49:57 +00:00
// Get the `client_id` and the `client_secret` values
app, err := getAppCredentials(BASE_URL)
2022-11-29 21:25:08 +00:00
if err != nil {
log.Fatal(err)
}
2022-11-30 16:49:57 +00:00
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 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)
2022-11-29 21:25:08 +00:00
}
2022-11-30 16:49:57 +00:00
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
2022-11-29 21:25:08 +00:00
contents, err := os.ReadFile("credentials.json")
if err != nil {
2022-11-30 16:49:57 +00:00
return createApp(instance)
2022-11-29 21:25:08 +00:00
}
2022-11-30 16:49:57 +00:00
// The file's contents are unmarshalled into a map
var credentials app
err = json.Unmarshal(contents, &credentials)
2022-11-29 21:25:08 +00:00
if err != nil {
2022-11-30 16:49:57 +00:00
return nil, err
2022-11-29 21:25:08 +00:00
}
2022-11-30 16:49:57 +00:00
return &credentials, nil
2022-11-29 21:25:08 +00:00
}
2022-11-30 16:49:57 +00:00
func createApp(instance string) (*app, error) {
// Define the values to the request for creating the app
2022-11-29 21:25:08 +00:00
data := url.Values{
"client_name": {"grames"},
2022-11-30 16:49:57 +00:00
"redirect_uris": {DEFAULT_REDIRECT_URI},
2022-11-29 21:25:08 +00:00
}
2022-11-30 16:49:57 +00:00
// Send request to create app
resp, err := http.PostForm(instance+"/api/v1/apps", data)
2022-11-29 21:25:08 +00:00
if err != nil {
2022-11-30 16:49:57 +00:00
return nil, err
2022-11-29 21:25:08 +00:00
}
defer resp.Body.Close()
2022-11-30 16:49:57 +00:00
// Read the body's JSON response into `contents` as []byte
2022-11-29 21:25:08 +00:00
contents, err := io.ReadAll(resp.Body)
if err != nil {
2022-11-30 16:49:57 +00:00
return nil, err
2022-11-29 21:25:08 +00:00
}
2022-11-30 16:49:57 +00:00
// Unmarshal the JSON data into an app struct
var credentials app
err = json.Unmarshal(contents, &credentials)
2022-11-29 21:25:08 +00:00
if err != nil {
2022-11-30 16:49:57 +00:00
return nil, err
2022-11-29 21:25:08 +00:00
}
2022-11-30 16:49:57 +00:00
// 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)
2022-11-29 21:25:08 +00:00
if err != nil {
2022-11-30 16:49:57 +00:00
return nil, err
2022-11-29 21:25:08 +00:00
}
2022-11-30 16:49:57 +00:00
return &credentials, nil
2022-11-29 21:25:08 +00:00
}