114 lines
2.9 KiB
Go
114 lines
2.9 KiB
Go
package apiclient
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
|
|
"git.jamestombleson.com/jtom38/newsbot-api/domain"
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
const (
|
|
UserBaseRoute = "api/v1/users"
|
|
)
|
|
|
|
type Users interface {
|
|
Login(username, password string) (domain.LoginResponse, error)
|
|
SignUp(username, password string) (domain.BaseResponse, error)
|
|
RefreshJwtToken(username, refreshToken string) (domain.LoginResponse, error)
|
|
RefreshJwtTokenFromContext(ctx echo.Context) (domain.LoginResponse, error)
|
|
RefreshSessionToken(jwtToken string) (domain.BaseResponse, error)
|
|
}
|
|
|
|
type userClient struct {
|
|
serverAddress string
|
|
client http.Client
|
|
}
|
|
|
|
func newUserService(serverAddress string) userClient {
|
|
return userClient{
|
|
serverAddress: serverAddress,
|
|
client: http.Client{},
|
|
}
|
|
}
|
|
|
|
func (a userClient) Login(username, password string) (domain.LoginResponse, error) {
|
|
endpoint := fmt.Sprintf("%s/%s/login", a.serverAddress, UserBaseRoute)
|
|
|
|
param := url.Values{}
|
|
param.Set("username", username)
|
|
param.Set("password", password)
|
|
|
|
// Create the struct we are expecting back from the API so we can have it populated
|
|
var bind = domain.LoginResponse{}
|
|
err := PostUrlForm(a.client, endpoint, param, &bind)
|
|
if err != nil {
|
|
return domain.LoginResponse{}, err
|
|
}
|
|
|
|
return bind, nil
|
|
}
|
|
|
|
func (a userClient) SignUp(username, password string) (domain.BaseResponse, error) {
|
|
endpoint := fmt.Sprintf("%s/%s/register", a.serverAddress, UserBaseRoute)
|
|
|
|
param := url.Values{}
|
|
param.Set("username", username)
|
|
param.Set("password", password)
|
|
|
|
// Create the struct we are expecting back from the API so we can have it populated
|
|
var bind = domain.BaseResponse{}
|
|
err := PostUrlForm(a.client, endpoint, param, &bind)
|
|
if err != nil {
|
|
return domain.BaseResponse{}, err
|
|
}
|
|
|
|
return bind, nil
|
|
}
|
|
|
|
func (a userClient) RefreshJwtToken(username, refreshToken string) (domain.LoginResponse, error) {
|
|
endpoint := fmt.Sprintf("%s/%s/refresh/token", a.serverAddress, UserBaseRoute)
|
|
body := domain.RefreshTokenRequest{
|
|
Username: username,
|
|
RefreshToken: refreshToken,
|
|
}
|
|
|
|
var bind = domain.LoginResponse{}
|
|
err := PostBodyUrl(a.client, endpoint, body, &bind)
|
|
if err != nil {
|
|
return domain.LoginResponse{}, err
|
|
}
|
|
|
|
return bind, nil
|
|
}
|
|
|
|
|
|
func (a userClient) RefreshJwtTokenFromContext(ctx echo.Context) (domain.LoginResponse, error) {
|
|
resp := domain.LoginResponse{}
|
|
|
|
username, err := ctx.Cookie("newsbot.user")
|
|
if err != nil {
|
|
return resp, err
|
|
}
|
|
|
|
refreshToken, err := ctx.Cookie("newsbot.refreshToken")
|
|
if err != nil {
|
|
return resp, err
|
|
}
|
|
|
|
return a.RefreshJwtToken(username.Value, refreshToken.Value)
|
|
}
|
|
|
|
func (a userClient) RefreshSessionToken(jwtToken string) (domain.BaseResponse, error) {
|
|
endpoint := fmt.Sprintf("%s/%s/refresh/sessionToken", a.serverAddress, UserBaseRoute)
|
|
|
|
var bind = domain.BaseResponse{}
|
|
err := PostUrlAuthorized(a.client, endpoint, jwtToken, &bind)
|
|
if err != nil {
|
|
return domain.BaseResponse{}, err
|
|
}
|
|
|
|
return bind, nil
|
|
}
|