Initial commit

This commit is contained in:
Pal Kerecsenyi 2023-10-21 12:02:42 +01:00
commit ea1cbb6857
Signed by: palk
GPG Key ID: 6891661E25394C2C
7 changed files with 131 additions and 0 deletions

0
.gitignore vendored Normal file
View File

1
README.md Normal file
View File

@ -0,0 +1 @@
# PalAuth IAM Go client

26
check.go Normal file
View File

@ -0,0 +1,26 @@
package palauthiam
import "context"
type CheckPermissionRequest struct {
UserID string
Permission string
}
type CheckPermissionResponse struct {
Allowed bool `json:"allowed"`
}
func (c *PalAuthIAMClient) CheckPermission(ctx context.Context, request *CheckPermissionRequest) (bool, error) {
resp := CheckPermissionResponse{}
err := c.getRequest(ctx, "/check", map[string]string{
"userId": request.UserID,
"permission": request.Permission,
}, &resp)
if err != nil {
return false, err
}
return resp.Allowed, nil
}

87
client.go Normal file
View File

@ -0,0 +1,87 @@
package palauthiam
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
)
type PalAuthIAMClient struct {
clientId string
clientSecret string
baseURL string
}
func InitClient(clientId, clientSecret string) *PalAuthIAMClient {
return &PalAuthIAMClient{
clientId: clientId,
clientSecret: clientSecret,
baseURL: "https://auth.palk.me/iam",
}
}
func (c *PalAuthIAMClient) SetBaseURL(baseURL string) {
c.baseURL = baseURL
}
func mapToQuery(variables map[string]string) (output string) {
for key, value := range variables {
output += fmt.Sprintf("%s=%s&", key, value)
}
if len(variables) != 0 {
output = strings.TrimSuffix(output, "&")
}
return output
}
func (c *PalAuthIAMClient) buildURL(path string, query map[string]string) string {
return fmt.Sprintf("%s%s?%s", c.baseURL, path, mapToQuery(query))
}
func (c *PalAuthIAMClient) runRequest(req *http.Request, mapTo interface{}) error {
res, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("making request: %s", err)
}
resBody, err := io.ReadAll(res.Body)
if err != nil {
return fmt.Errorf("read response body: %s", err)
}
if res.StatusCode >= 200 && res.StatusCode < 300 {
err = json.Unmarshal(resBody, mapTo)
if err != nil {
return fmt.Errorf("decode JSON response: %s", err)
}
} else if res.StatusCode >= 400 && res.StatusCode < 500 {
return &IAMError{
ServerMessage: string(resBody),
}
}
return nil
}
func (c *PalAuthIAMClient) getRequest(
ctx context.Context,
path string,
variables map[string]string,
mapTo interface{},
) error {
req, err := http.NewRequestWithContext(
ctx,
http.MethodGet,
c.buildURL(path, variables),
http.NoBody,
)
if err != nil {
return fmt.Errorf("construct request: %s", err)
}
return c.runRequest(req, mapTo)
}

11
errors.go Normal file
View File

@ -0,0 +1,11 @@
package palauthiam
import "fmt"
type IAMError struct {
ServerMessage string
}
func (r *IAMError) Error() string {
return fmt.Sprintf("iam error: %s", r.ServerMessage)
}

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module gitlab.palk.me/paltiverse/palauth-iam-go
go 1.21.1

3
types.go Normal file
View File

@ -0,0 +1,3 @@
package palauthiam
type IAMPermission string