From ea1cbb68577c325faf7b336f12628818e39577d7 Mon Sep 17 00:00:00 2001
From: Pal Kerecsenyi <pal@p.kerecs.com>
Date: Sat, 21 Oct 2023 12:02:42 +0100
Subject: [PATCH] Initial commit

---
 .gitignore |  0
 README.md  |  1 +
 check.go   | 26 ++++++++++++++++
 client.go  | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 errors.go  | 11 +++++++
 go.mod     |  3 ++
 types.go   |  3 ++
 7 files changed, 131 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 README.md
 create mode 100644 check.go
 create mode 100644 client.go
 create mode 100644 errors.go
 create mode 100644 go.mod
 create mode 100644 types.go

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..e69de29
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..85d261c
--- /dev/null
+++ b/README.md
@@ -0,0 +1 @@
+# PalAuth IAM Go client
diff --git a/check.go b/check.go
new file mode 100644
index 0000000..f6185e4
--- /dev/null
+++ b/check.go
@@ -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
+}
diff --git a/client.go b/client.go
new file mode 100644
index 0000000..53814ba
--- /dev/null
+++ b/client.go
@@ -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)
+}
diff --git a/errors.go b/errors.go
new file mode 100644
index 0000000..e205249
--- /dev/null
+++ b/errors.go
@@ -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)
+}
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..cd0eb9f
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,3 @@
+module gitlab.palk.me/paltiverse/palauth-iam-go
+
+go 1.21.1
diff --git a/types.go b/types.go
new file mode 100644
index 0000000..9cb18bb
--- /dev/null
+++ b/types.go
@@ -0,0 +1,3 @@
+package palauthiam
+
+type IAMPermission string