Added sign up support. Tested on mastodon

But required test on GoToSocial.
Confirmation inside bloat-fe not supported
This commit is contained in:
localhost_frssoft 2022-11-13 00:00:02 +03:00
parent f9702f81a9
commit 0f060f09c9
6 changed files with 244 additions and 2 deletions

View file

@ -3,10 +3,13 @@ package mastodon
import (
"context"
"fmt"
"encoding/json"
"net/http"
"net/url"
"strconv"
"time"
"strings"
"path"
)
type AccountPleroma struct {
@ -59,6 +62,71 @@ type AccountSource struct {
Fields *[]Field `json:"fields"`
}
type RegisterCredintals struct {
http.Client
Server string
App *Application
Reason string
Username string
Email string
Password string
Agreement bool
Locale string
}
// Use for register account on GoToSocial
type Registred struct {
AccessToken string `json:"access_token"`
CreatedAt int64 `json:"created_at"`
Scope string `json:"scope"`
TokenType string `json:"token_type"`
}
// RegisterAccount create new account
func RegisterAccount(ctx context.Context, instance string, reason string, username string, email string, password string, agreement bool, locale string, registerCrendintals RegisterCredintals, bearer string) (*string, error) {
var registred Registred
params := url.Values{}
params.Set("reason", reason)
params.Set("username", username)
params.Set("email", email)
params.Set("password", password)
params.Set("agreement", strconv.FormatBool(agreement))
params.Set("locale", locale)
u, err := url.Parse("https://" + instance)
if err != nil {
return nil, err
}
u.Path = path.Join(u.Path, "/api/v1/accounts")
req, err := http.NewRequest(http.MethodPost, u.String(), strings.NewReader(params.Encode()))
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Authorization", "Bearer "+bearer)
resp, err := registerCrendintals.Do(req)
fmt.Println(req)
fmt.Println(resp)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, parseAPIError("bad request", resp)
}
err = json.NewDecoder(resp.Body).Decode(&registred)
fmt.Println(resp.Body)
if err != nil {
return nil, err
}
return &registred.AccessToken, nil
}
// GetAccount return Account.
func (c *Client) GetAccount(ctx context.Context, id string) (*Account, error) {
var account Account

View file

@ -48,7 +48,6 @@ func RegisterApp(ctx context.Context, appConfig *AppConfig) (*Application, error
params.Set("redirect_uris", appConfig.RedirectURIs)
}
params.Set("scopes", appConfig.Scopes)
params.Set("website", "https://gitea.phreedom.club/localhost_frssoft/bloat/src/branch/localhost_custom")
u, err := url.Parse(appConfig.Server)
if err != nil {
@ -71,7 +70,7 @@ func RegisterApp(ctx context.Context, appConfig *AppConfig) (*Application, error
if resp.StatusCode != http.StatusOK {
return nil, parseAPIError("bad request", resp)
}
var app Application
err = json.NewDecoder(resp.Body).Decode(&app)
if err != nil {
@ -94,3 +93,50 @@ func RegisterApp(ctx context.Context, appConfig *AppConfig) (*Application, error
return &app, nil
}
type AppAuth struct {
http.Client
}
// RegisterApp make auth application and return app token.
func AuthApp(ctx context.Context, appConfig *Application, instance string) (*string, error) {
var appAuth AppAuth
params := url.Values{}
params.Set("client_id", appConfig.ClientID)
params.Set("client_secret", appConfig.ClientSecret)
params.Set("redirect_uris", "urn:ietf:wg:oauth:2.0:oob")
params.Set("grant_type", "client_credentials")
params.Set("scope", "read write follow")
u, err := url.Parse("https://" + instance)
if err != nil {
return nil, err
}
u.Path = path.Join(u.Path, "/oauth/token")
req, err := http.NewRequest(http.MethodPost, u.String(), strings.NewReader(params.Encode()))
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := appAuth.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, parseAPIError("bad request", resp)
}
var res struct {
AccessToken string `json:"access_token"`
}
err = json.NewDecoder(resp.Body).Decode(&res)
if err != nil {
return nil, err
}
return &res.AccessToken, nil
}

View file

@ -202,6 +202,8 @@ func (c *Client) Authenticate(ctx context.Context, username, password string) er
return c.authenticate(ctx, params)
}
// AuthenticateToken logs in using a grant token returned by Application.AuthURI.
//
// redirectURI should be the same as Application.RedirectURI.