chore: Adds login and user creation support to the API.
This commit is contained in:
parent
96833b238b
commit
843cd34785
13 changed files with 683 additions and 12 deletions
21
apiclient/apiclient.go
Normal file
21
apiclient/apiclient.go
Normal file
|
@ -0,0 +1,21 @@
|
|||
package apiclient
|
||||
|
||||
import "git.terah.dev/imterah/hermes/apiclient/users"
|
||||
|
||||
type HermesAPIClient struct {
|
||||
URL string
|
||||
}
|
||||
|
||||
/// Users
|
||||
|
||||
func (api *HermesAPIClient) UserGetRefreshToken(username *string, email *string, password string) (string, error) {
|
||||
return users.GetRefreshToken(api.URL, username, email, password)
|
||||
}
|
||||
|
||||
func (api *HermesAPIClient) UserGetJWTFromToken(refreshToken string) (string, error) {
|
||||
return users.GetJWTFromToken(api.URL, refreshToken)
|
||||
}
|
||||
|
||||
func (api *HermesAPIClient) UserCreate(fullName, username, email, password string, isBot bool) (string, error) {
|
||||
return users.CreateUser(api.URL, fullName, username, email, password, isBot)
|
||||
}
|
102
apiclient/backendstructs/struct.go
Normal file
102
apiclient/backendstructs/struct.go
Normal file
|
@ -0,0 +1,102 @@
|
|||
package backendstructs
|
||||
|
||||
type BackendCreationRequest struct {
|
||||
Token string `validate:"required"`
|
||||
Name string `validate:"required"`
|
||||
Description *string `json:"description"`
|
||||
Backend string `validate:"required"`
|
||||
BackendParameters interface{} `json:"connectionDetails" validate:"required"`
|
||||
}
|
||||
|
||||
type BackendLookupRequest struct {
|
||||
Token string `validate:"required"`
|
||||
BackendID *uint `json:"id"`
|
||||
Name *string `json:"name"`
|
||||
Description *string `json:"description"`
|
||||
Backend *string `json:"backend"`
|
||||
}
|
||||
|
||||
type BackendRemovalRequest struct {
|
||||
Token string `validate:"required"`
|
||||
BackendID uint `json:"id" validate:"required"`
|
||||
}
|
||||
|
||||
type ConnectionsRequest struct {
|
||||
Token string `validate:"required" json:"token"`
|
||||
Id uint `validate:"required" json:"id"`
|
||||
}
|
||||
|
||||
type ProxyCreationRequest struct {
|
||||
Token string `validate:"required" json:"token"`
|
||||
Name string `validate:"required" json:"name"`
|
||||
Description *string `json:"description"`
|
||||
Protocol string `validate:"required" json:"protocol"`
|
||||
SourceIP string `validate:"required" json:"sourceIP"`
|
||||
SourcePort uint16 `validate:"required" json:"sourcePort"`
|
||||
DestinationPort uint16 `validate:"required" json:"destinationPort"`
|
||||
ProviderID uint `validate:"required" json:"providerID"`
|
||||
AutoStart *bool `json:"autoStart"`
|
||||
}
|
||||
|
||||
type ProxyLookupRequest struct {
|
||||
Token string `validate:"required" json:"token"`
|
||||
Id *uint `json:"id"`
|
||||
Name *string `json:"name"`
|
||||
Description *string `json:"description"`
|
||||
Protocol *string `json:"protocol"`
|
||||
SourceIP *string `json:"sourceIP"`
|
||||
SourcePort *uint16 `json:"sourcePort"`
|
||||
DestinationPort *uint16 `json:"destPort"`
|
||||
ProviderID *uint `json:"providerID"`
|
||||
AutoStart *bool `json:"autoStart"`
|
||||
}
|
||||
|
||||
type ProxyRemovalRequest struct {
|
||||
Token string `validate:"required" json:"token"`
|
||||
ID uint `validate:"required" json:"id"`
|
||||
}
|
||||
|
||||
type ProxyStartRequest struct {
|
||||
Token string `validate:"required" json:"token"`
|
||||
ID uint `validate:"required" json:"id"`
|
||||
}
|
||||
|
||||
type ProxyStopRequest struct {
|
||||
Token string `validate:"required" json:"token"`
|
||||
ID uint `validate:"required" json:"id"`
|
||||
}
|
||||
|
||||
type UserCreationRequest struct {
|
||||
Name string `json:"name" validate:"required"`
|
||||
Email string `json:"email" validate:"required"`
|
||||
Password string `json:"password" validate:"required"`
|
||||
Username string `json:"username" validate:"required"`
|
||||
|
||||
ExistingUserToken string `json:"token"`
|
||||
IsBot bool `json:"isBot"`
|
||||
}
|
||||
|
||||
type UserLoginRequest struct {
|
||||
Username *string `json:"username"`
|
||||
Email *string `json:"email"`
|
||||
|
||||
Password string `json:"password" validate:"required"`
|
||||
}
|
||||
|
||||
type UserLookupRequest struct {
|
||||
Token string `validate:"required"`
|
||||
UID *uint `json:"id"`
|
||||
Name *string `json:"name"`
|
||||
Email *string `json:"email"`
|
||||
Username *string `json:"username"`
|
||||
IsBot *bool `json:"isServiceAccount"`
|
||||
}
|
||||
|
||||
type UserRefreshRequest struct {
|
||||
Token string `json:"token" validate:"required"`
|
||||
}
|
||||
|
||||
type UserRemovalRequest struct {
|
||||
Token string `json:"token" validate:"required"`
|
||||
UID *uint `json:"uid"`
|
||||
}
|
99
apiclient/users/auth.go
Normal file
99
apiclient/users/auth.go
Normal file
|
@ -0,0 +1,99 @@
|
|||
package users
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"git.terah.dev/imterah/hermes/apiclient/backendstructs"
|
||||
)
|
||||
|
||||
type refreshTokenResponse struct {
|
||||
Success bool `json:"success"`
|
||||
RefreshToken string `json:"refreshToken"`
|
||||
}
|
||||
|
||||
type jwtTokenResponse struct {
|
||||
Success bool `json:"success"`
|
||||
JWT string `json:"token"`
|
||||
}
|
||||
|
||||
func GetRefreshToken(url string, username, email *string, password string) (string, error) {
|
||||
body, err := json.Marshal(&backendstructs.UserLoginRequest{
|
||||
Username: username,
|
||||
Email: email,
|
||||
Password: password,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
res, err := http.Post(fmt.Sprintf("%s/api/v1/users/login", url), "application/json", bytes.NewBuffer(body))
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
bodyContents, err := io.ReadAll(res.Body)
|
||||
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to read response body: %s", err.Error())
|
||||
}
|
||||
|
||||
response := &refreshTokenResponse{}
|
||||
|
||||
if err := json.Unmarshal(bodyContents, response); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if !response.Success {
|
||||
return "", fmt.Errorf("failed to get refresh token")
|
||||
}
|
||||
|
||||
if response.RefreshToken == "" {
|
||||
return "", fmt.Errorf("refresh token is empty")
|
||||
}
|
||||
|
||||
return response.RefreshToken, nil
|
||||
}
|
||||
|
||||
func GetJWTFromToken(url, refreshToken string) (string, error) {
|
||||
body, err := json.Marshal(&backendstructs.UserRefreshRequest{
|
||||
Token: refreshToken,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
res, err := http.Post(fmt.Sprintf("%s/api/v1/users/refresh", url), "application/json", bytes.NewBuffer(body))
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
bodyContents, err := io.ReadAll(res.Body)
|
||||
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to read response body: %s", err.Error())
|
||||
}
|
||||
|
||||
response := &jwtTokenResponse{}
|
||||
|
||||
if err := json.Unmarshal(bodyContents, response); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if !response.Success {
|
||||
return "", fmt.Errorf("failed to get JWT token")
|
||||
}
|
||||
|
||||
if response.JWT == "" {
|
||||
return "", fmt.Errorf("JWT token is empty")
|
||||
}
|
||||
|
||||
return response.JWT, nil
|
||||
}
|
63
apiclient/users/create.go
Normal file
63
apiclient/users/create.go
Normal file
|
@ -0,0 +1,63 @@
|
|||
package users
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"git.terah.dev/imterah/hermes/apiclient/backendstructs"
|
||||
)
|
||||
|
||||
type createUserResponse struct {
|
||||
Error string `json:"error"`
|
||||
Success bool `json:"success"`
|
||||
RefreshToken string `json:"refreshToken"`
|
||||
}
|
||||
|
||||
func CreateUser(url, fullName, username, email, password string, isBot bool) (string, error) {
|
||||
body, err := json.Marshal(&backendstructs.UserCreationRequest{
|
||||
Username: username,
|
||||
Name: fullName,
|
||||
Email: email,
|
||||
Password: password,
|
||||
IsBot: isBot,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
res, err := http.Post(fmt.Sprintf("%s/api/v1/users/create", url), "application/json", bytes.NewBuffer(body))
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
bodyContents, err := io.ReadAll(res.Body)
|
||||
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to read response body: %s", err.Error())
|
||||
}
|
||||
|
||||
response := &createUserResponse{}
|
||||
|
||||
if err := json.Unmarshal(bodyContents, response); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if response.Error != "" {
|
||||
return "", fmt.Errorf("error from server: %s", response.Error)
|
||||
}
|
||||
|
||||
if !response.Success {
|
||||
return "", fmt.Errorf("failed to get refresh token")
|
||||
}
|
||||
|
||||
if response.RefreshToken == "" {
|
||||
return "", fmt.Errorf("refresh token is empty")
|
||||
}
|
||||
|
||||
return response.RefreshToken, nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue