forked from GoBelieveIO/im_service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimauth.go
88 lines (72 loc) · 2.09 KB
/
imauth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"io"
"io/ioutil"
"net/http"
"encoding/json"
"log"
"fmt"
"github.com/garyburd/redigo/redis"
"github.com/favframework/debug"
"github.com/bitly/go-simplejson"
)
// hello world, the web server
func AuthGrant(w http.ResponseWriter, req *http.Request) {
log.Print("post group notification")
body, err := ioutil.ReadAll(req.Body)
obj, err := simplejson.NewJson(body)
if err != nil {
log.Print("error:", err)
//WriteHttpError(400, "invalid json format", w)
return
}
uid, err := obj.Get("uid").Int64()
app_id, err := obj.Get("app_id").Int64()
user_name, err := obj.Get("user_name").String()
platform_id, err := obj.Get("platform_id").String()
device_id, err := obj.Get("device_id").String()
token, err := obj.Get("token").String()
if err != nil {
log.Print("error:", err)
//WriteHttpError(400, "invalid json format", w)
io.WriteString(w, "invalid token")
return
}
c, err := redis.Dial("tcp", "127.0.0.1:6379")
if err != nil {
fmt.Println("Connect to redis error", err)
return
}
defer c.Close()
_, err = c.Do("hset", "access_token_"+token, "user_id", uid)
_, err = c.Do("hset", "access_token_"+token, "app_id", app_id)
_, err = c.Do("hset", "access_token_"+token, "platform_id", platform_id)
_, err = c.Do("hset", "access_token_"+token, "device_id", device_id)
if err != nil {
fmt.Println("haset failed", err.Error())
}
userId, err := redis.String(c.Do("HGET", "access_token_"+token,"user_id"))
if err != nil {
fmt.Println("redis get failed:", err)
} else {
fmt.Printf("get user_id: %v \n", userId)
fmt.Printf("set token: %v \n", token)
godump.Dump(user_name)
//io.WriteString(w, "set token:"+token)
w.Header().Set("Content-Type", "application/json")
obj := make(map[string]interface{})
obj["token"] = token
b, _ := json.Marshal(obj)
w.Write(b)
}
}
func main() {
log.Print("Starting AuthGrant Service: ")
http.HandleFunc("/auth/grant", AuthGrant)
http.HandleFunc("/set/token", AuthGrant)
http.HandleFunc("/auth/token", AuthGrant)
err := http.ListenAndServe(":8001", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}