thisnthat 5 years ago
parent
commit
f865ccfe51
5 changed files with 38 additions and 37 deletions
  1. 0 7
      client.go
  2. 0 22
      discourse.go
  3. 2 1
      groups.go
  4. 24 0
      sso.go
  5. 12 7
      users.go

+ 0 - 7
client.go

@@ -1,7 +0,0 @@
-package discourse
-
-type Config struct {
-	Endpoint string
-	ApiKey   string
-	ApiUser  string
-}

+ 0 - 22
discourse.go

@@ -1,29 +1,7 @@
 package discourse
 
-import "github.com/google/uuid"
-
 type ApiConfig struct {
 	Endpoint string
 	ApiKey   string
 	ApiUser  string
 }
-
-type SsoConfig struct {
-	Endpoint       string
-	ReturnEndpoint string
-	Secret         string
-	Uuid           uuid.UUID
-}
-
-func GetSSORequestURL(config SsoConfig) string {
-	url := generateSSORequestURL(config)
-
-	return url
-}
-
-// ValidateSsoResponse - Validate the signature on the sso response
-func ValidateSsoResponse(sso, sig, secret string) bool {
-	ssoDataSig := computeHmac256(sso, secret)
-
-	return sig == ssoDataSig
-}

+ 2 - 1
groups.go

@@ -1,6 +1,7 @@
 package discourse
 
+// Group - A Discourse group
 type Group struct {
 	ID   int    `json:"id"`
-	name string `json:"name"`
+	Name string `json:"name"`
 }

+ 24 - 0
sso.go

@@ -7,8 +7,32 @@ import (
 	"encoding/hex"
 	"fmt"
 	"net/url"
+
+	"github.com/google/uuid"
 )
 
+// SsoConfig - Configuration for an SSO Request
+type SsoConfig struct {
+	Endpoint       string
+	ReturnEndpoint string
+	Secret         string
+	Uuid           uuid.UUID
+}
+
+// GetSSORequestURL - gets a url for an sso request
+func GetSSORequestURL(config SsoConfig) string {
+	url := generateSSORequestURL(config)
+
+	return url
+}
+
+// ValidateSsoResponse - Validate the signature on the sso response
+func ValidateSsoResponse(sso, sig, secret string) bool {
+	ssoDataSig := computeHmac256(sso, secret)
+
+	return sig == ssoDataSig
+}
+
 func generateSSORequestURL(config SsoConfig) string {
 	// Build the query string payload
 	payload := fmt.Sprintf("nonce=%s&return_sso_url=%s", config.Uuid, config.ReturnEndpoint)

+ 12 - 7
users.go

@@ -6,26 +6,31 @@ import (
 	"net/http"
 )
 
+// UserResponse - Structure of a discourse user api response
 type UserResponse struct {
-	User User `json:"user"`
+	User      User     `json:"user"`
+	Errors    []string `json:"errors"`
+	ErrorType string   `json:"error_type"`
 }
 
+// User - A discoruse User
 type User struct {
-	ID        string  `json:"id"`
+	ID        int     `json:"id"`
 	Username  string  `json:"username"`
+	CanSendPM bool    `json:"can_send_private_messages"`
 	Moderator bool    `json:"moderator"`
 	Admin     bool    `json:"admin"`
-	Groups    []Group `json:"group"`
+	Groups    []Group `json:"groups"`
 }
 
-func GetUser(config ApiConfig, username string) {
+// GetUser - Get a discourse user
+func GetUser(config ApiConfig, username string) User {
 	url := fmt.Sprintf("%s/users/%s.json?api_key=%s&api_username=%s", config.Endpoint, username, config.ApiKey, config.ApiUser)
-	fmt.Println(url)
 
 	response, _ := http.Get(url)
 
-	var result User
+	var result *UserResponse
 	json.NewDecoder(response.Body).Decode(&result)
 
-	fmt.Println(result)
+	return result.User
 }