From 804268f298028811b0d3b996f2b91d159ba63306 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Mon, 19 Feb 2024 15:06:25 +0000 Subject: [PATCH] add registration function --- .golangci.yaml | 3 +- main.go | 111 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 111 insertions(+), 3 deletions(-) diff --git a/.golangci.yaml b/.golangci.yaml index 8549273..5998567 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -18,5 +18,6 @@ linters-settings: linters: enable-all: true - # disable: + disable: + #- json fast: false diff --git a/main.go b/main.go index 55d03a6..e87ee60 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,114 @@ package main -import "fmt" +import ( + "bytes" + "context" + "encoding/json" + "fmt" + "net/http" + "time" +) func main() { - fmt.Println("Hello, Enbas!") + register() +} + +const ( + applicationName = "Enbas" + redirectUris = "urn:ietf:wg:oauth:2.0:oob" +) + +type AccountConfig struct { + CurrentAccount string `json:"currentAccount"` + Accounts map[string]Account `json:"accounts"` +} + +type Account struct { + Instance string `json:"instance"` + ClientID string `json:"clientId"` + ClientSecret string `json:"clientSecret"` + AccessToken string `json:"accessToken"` +} + +type RegisterRequest struct { + ClientName string `json:"client_name"` + RedirectUris string `json:"redirect_uris"` + Scopes string `json:"scopes"` + Website string `json:"website"` +} + +type RegisterResponse struct { + ClientID string `json:"client_id"` + ClientSecret string `json:"client_secret"` + ID string `json:"id"` + Name string `json:"name"` + RedirectUri string `json:"redirect_uri"` + VapidKey string `json:"vapid_key"` + Website string `json:"website"` +} + +func register() Account { + // ask user for instance, if not start with http(s), prepend with https. + fmt.Print("Please enter the instance URL: ") + + instance := "" + if _, err := fmt.Scanln(&instance); err != nil { + panic(err) + } + + request := RegisterRequest{ + ClientName: applicationName, + RedirectUris: redirectUris, + Scopes: "read", + Website: "", + } + + data, err := json.Marshal(request) + if err != nil { + panic(err) + } + + httpRequestBody := bytes.NewBuffer(data) + + path := "/api/v1/apps" + url := instance + path + + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + httpRequest, err := http.NewRequestWithContext(ctx, http.MethodPost, url, httpRequestBody) + if err != nil { + panic(err) + } + + httpRequest.Header.Add("Content-Type", "application/json") + httpRequest.Header.Set("User-Agent", "Enbas/0.0.0") + + httpClient := http.Client{} + + httpResponse, err := httpClient.Do(httpRequest) + if err != nil { + panic(err) + } + + defer httpResponse.Body.Close() + + var registerResponse RegisterResponse + + if err := json.NewDecoder(httpResponse.Body).Decode(®isterResponse); err != nil { + panic(err) + } + + fmt.Println(registerResponse) + + fmt.Println("Please sign into your instance with your browser and enter the access token\nAccess token: ") + + account := Account{ + Instance: instance, + ClientID: registerResponse.ClientID, + ClientSecret: registerResponse.ClientSecret, + AccessToken: "", + } + + return account }