Matheus 6 years ago
parent
commit
bc63443862
3 changed files with 92 additions and 0 deletions
  1. 34 0
      src/lib/authentication.go
  2. 51 0
      src/lib/curl.go
  3. 7 0
      src/lib/error.go

+ 34 - 0
src/lib/authentication.go

@@ -0,0 +1,34 @@
1
+package lib
2
+
3
+import (
4
+	"net/http"
5
+	"strconv"
6
+)
7
+
8
+type UserData struct {
9
+	Id_user   string
10
+	Email     string
11
+	Name      string
12
+	Username  string
13
+	Token     string
14
+	Url_token string
15
+	Avatar    string
16
+	Language  string
17
+	Errors    []apiError
18
+}
19
+
20
+func (user UserData) getId() int {
21
+	id, _ := strconv.Atoi(user.Id_user)
22
+	return id
23
+}
24
+
25
+func GetUser(r *http.Request) *UserData {
26
+
27
+	auth := r.Header.Get("Auth")
28
+	userData := curl(auth)
29
+
30
+	// debug only
31
+	//fmt.Printf("%+v\n", userData)
32
+
33
+	return userData
34
+}

+ 51 - 0
src/lib/curl.go

@@ -0,0 +1,51 @@
1
+package lib
2
+
3
+import (
4
+	"encoding/json"
5
+	"io/ioutil"
6
+	"log"
7
+	"net/http"
8
+)
9
+
10
+func curl(auth string) *UserData {
11
+
12
+	req, err := http.NewRequest("POST", "http://208.97.141.22/api/sign/user_data", nil)
13
+
14
+	if err != nil {
15
+		return nil
16
+	}
17
+
18
+	req.Header.Set("Auth", auth)
19
+
20
+	resp, err := http.DefaultClient.Do(req)
21
+
22
+	if err != nil {
23
+		log.Print(err)
24
+		return nil
25
+	}
26
+
27
+	defer resp.Body.Close()
28
+
29
+	body, _ := ioutil.ReadAll(resp.Body)
30
+
31
+	// debug only
32
+	//s := string(body[:])
33
+	//fmt.Println(s)
34
+
35
+	var userData UserData
36
+
37
+	err = json.Unmarshal(body, &userData)
38
+
39
+	//fmt.Printf("%+v\n", userData)
40
+
41
+	if err != nil {
42
+		log.Print(err)
43
+		return nil
44
+	}
45
+
46
+	if len(userData.Errors) > 0 {
47
+		return nil
48
+	}
49
+
50
+	return &userData
51
+}

+ 7 - 0
src/lib/error.go

@@ -0,0 +1,7 @@
1
+package lib
2
+
3
+type apiError struct {
4
+	ErrorType string `json:type`
5
+	Parameter string `json:parameter`
6
+	Message   string `json:message`
7
+}