Files
netris-nestri/packages/maitred/pkg/auth/auth.go
Wanjohi 060718d8b0 feat: Update website, API, and infra (#164)
>Adds `maitred` in charge of handling automated game installs, updates,
and even execution.

>Not only that, we have the hosted stuff here
>- [x] AWS Task on ECS GPUs
>- [ ] Add a service to listen for game starts and stops
(docker-compose.yml)
>- [x] Add a queue for requesting a game to start
>- [x] Fix up the play/watch UI 

>TODO:
>- Add a README
>- Add an SST docs

Edit:

- This adds a new landing page, updates the homepage etc etc
>I forgot what the rest of the updated stuff are 😅
2025-02-11 12:26:35 +03:00

59 lines
1.3 KiB
Go

package auth
import (
"encoding/json"
"fmt"
"io"
"nestri/maitred/pkg/resource"
"net/http"
"net/url"
"os"
"os/exec"
"github.com/charmbracelet/log"
)
type UserCredentials struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
}
func FetchUserToken(teamSlug string) (*UserCredentials, error) {
hostname, err := os.Hostname()
if err != nil {
log.Fatal("Could not get the hostname")
}
data := url.Values{}
data.Set("grant_type", "client_credentials")
data.Set("client_id", "device")
data.Set("client_secret", resource.Resource.AuthFingerprintKey.Value)
data.Set("team", teamSlug)
data.Set("hostname", hostname)
data.Set("provider", "device")
resp, err := http.PostForm(resource.Resource.Auth.Url+"/token", data)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
body, _ := io.ReadAll(resp.Body)
fmt.Println(string(body))
return nil, fmt.Errorf("failed to auth: " + string(body))
}
credentials := UserCredentials{}
err = json.NewDecoder(resp.Body).Decode(&credentials)
if err != nil {
return nil, err
}
return &credentials, nil
}
func GetHostname() string {
cmd, err := exec.Command("cat", "/etc/hostname").Output()
if err != nil {
log.Error("error getting container hostname", "err", err)
}
output := string(cmd)
return output
}