mirror of
https://github.com/nestriness/nestri.git
synced 2025-12-13 01:05:37 +02:00
## Description We are attempting to hookup maitred to the API Maitred duties will be: - [ ] Hookup to the API - [ ] Wait for signal (from the API) to start Steam - [ ] Stop signal to stop the gaming session, clean up Steam... and maybe do the backup ## Summary by CodeRabbit - **New Features** - Introduced Docker-based deployment configurations for both the main and relay applications. - Added new API endpoints enabling real-time machine messaging and enhanced IoT operations. - Expanded database schema and actor types to support improved machine tracking. - **Improvements** - Enhanced real-time communication and relay management with streamlined room handling. - Upgraded dependencies, logging, and error handling for greater stability and performance. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: DatCaptainHorse <DatCaptainHorse@users.noreply.github.com> Co-authored-by: Kristian Ollikainen <14197772+DatCaptainHorse@users.noreply.github.com>
46 lines
1.1 KiB
Go
46 lines
1.1 KiB
Go
package auth
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"log/slog"
|
|
"nestri/maitred/internal/resource"
|
|
"net/http"
|
|
"net/url"
|
|
)
|
|
|
|
type UserCredentials struct {
|
|
AccessToken string `json:"access_token"`
|
|
RefreshToken string `json:"refresh_token"`
|
|
}
|
|
|
|
func FetchUserToken(machineID string, resource *resource.Resource) (*UserCredentials, error) {
|
|
data := url.Values{}
|
|
data.Set("grant_type", "client_credentials")
|
|
data.Set("client_id", "maitred")
|
|
data.Set("client_secret", resource.AuthFingerprintKey.Value)
|
|
data.Set("fingerprint", machineID)
|
|
data.Set("provider", "machine")
|
|
resp, err := http.PostForm(resource.Auth.Url+"/token", data)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer func(Body io.ReadCloser) {
|
|
err = Body.Close()
|
|
if err != nil {
|
|
slog.Error("Error closing body", "err", err)
|
|
}
|
|
}(resp.Body)
|
|
if resp.StatusCode != 200 {
|
|
body, _ := io.ReadAll(resp.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
|
|
}
|