update auth.go, database.go, docs.go, forum.go, go.mod, go.sum, main.go, post_schema.go, role.go, server.go, service.go, swagger.json, swagger.yaml, thread_schema.go
This commit is contained in:
1 parent
42c7f5795d
commit
43505548f7
14 files changed
+855
No files matched your search
@@ -0,0 +1,156 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"esforum"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
const AUTH_URL = "https://auth.easystartup.su/api"
|
||||
|
||||
type AuthService struct {
|
||||
}
|
||||
|
||||
func NewAuthService() *AuthService {
|
||||
return &AuthService{}
|
||||
}
|
||||
|
||||
func (s *AuthService) Authorize(header string) (*esforum.AuthorizedUser, error) {
|
||||
client := &http.Client{}
|
||||
req, err := http.NewRequest("GET", AUTH_URL+"/user/self", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Add("Authorization", header)
|
||||
req.Header.Add("Accept", "application/json")
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func(Body io.ReadCloser) {
|
||||
err := Body.Close()
|
||||
if err != nil {
|
||||
|
||||
}
|
||||
}(resp.Body)
|
||||
|
||||
// Check if the response status is 200 OK
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("failed to authozire: %d %s\nheader: %s", resp.StatusCode, resp.Status, header)
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result esforum.AuthorizedUser
|
||||
err = json.Unmarshal(body, &result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
func (s *AuthService) AuthorizeById(id int64) (*esforum.AuthorizedUser, error) {
|
||||
client := &http.Client{}
|
||||
req, err := http.NewRequest("GET", fmt.Sprintf(AUTH_URL+"/user/%d", id), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Add("Authorization", "Bearer "+os.Getenv("ADMIN_TOKEN"))
|
||||
req.Header.Add("Accept", "application/json")
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func(Body io.ReadCloser) {
|
||||
err := Body.Close()
|
||||
if err != nil {
|
||||
|
||||
}
|
||||
}(resp.Body)
|
||||
|
||||
// Check if the response status is 200 OK
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("failed to authozire: %d %s", resp.StatusCode, resp.Status)
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result esforum.AuthorizedUser
|
||||
err = json.Unmarshal(body, &result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
func (s *AuthService) AuthorizeAndUpdatePicture(header string, picUrl string) (*esforum.AuthorizedUser, error) {
|
||||
client := &http.Client{}
|
||||
req, err := http.NewRequest("GET", AUTH_URL+"/user/self", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Add("Authorization", header)
|
||||
req.Header.Add("Accept", "application/json")
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func(Body io.ReadCloser) {
|
||||
err := Body.Close()
|
||||
if err != nil {
|
||||
|
||||
}
|
||||
}(resp.Body)
|
||||
|
||||
// Check if the response status is 200 OK
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("failed to authozire: %d %s\nheader: %s", resp.StatusCode, resp.Status, header)
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result esforum.AuthorizedUser
|
||||
err = json.Unmarshal(body, &result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result.PictureURL = picUrl
|
||||
requestBody, _ := json.Marshal(result)
|
||||
|
||||
req, err = http.NewRequest("POST", AUTH_URL+"/user/self", bytes.NewBuffer(requestBody))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Add("Authorization", header)
|
||||
req.Header.Add("Accept", "application/json")
|
||||
|
||||
_, err = client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &result, nil
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"esforum"
|
||||
)
|
||||
|
||||
type Authorization interface {
|
||||
Authorize(header string) (*esforum.AuthorizedUser, error)
|
||||
AuthorizeById(id int64) (*esforum.AuthorizedUser, error)
|
||||
AuthorizeAndUpdatePicture(header string, picUrl string) (*esforum.AuthorizedUser, error)
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
Authorization
|
||||
}
|
||||
|
||||
func NewService() *Service {
|
||||
return &Service{
|
||||
Authorization: NewAuthService(),
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user