This repository has been archived by the owner on Apr 8, 2020. It is now read-only.
/
AuthenticateWithTokenMessage.go
95 lines (78 loc) · 2.2 KB
/
AuthenticateWithTokenMessage.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
89
90
91
92
93
94
95
package handlers
import (
"code.google.com/p/goprotobuf/proto"
"errors"
"git.cloudrack.io/aiw3/np-server/environment"
"git.cloudrack.io/aiw3/np-server/np/protocol"
"git.cloudrack.io/aiw3/np-server/np/reply"
"git.cloudrack.io/aiw3/np-server/np/storage"
"git.cloudrack.io/aiw3/np-server/np/structs"
"github.com/pzduniak/logger"
"net"
"strconv"
"strings"
"time"
)
var WrongIPAddress = errors.New("Wrong IP address")
func RPCAuthenticateWithTokenMessage(conn net.Conn, connection_data *structs.ConnData, packet_data *structs.PacketData) error {
// Unmarshal the data
msg := new(protocol.AuthenticateWithTokenMessage)
err := proto.Unmarshal(packet_data.Content, msg)
if err != nil {
return err
}
// Split the token
token := string(msg.Token)
parts := strings.Split(token, ":")
// uid is the first part
id, err := strconv.Atoi(parts[0])
if err != nil {
return err
}
// Convert it to the Npid
npid := structs.IdToNpid(id)
// Set connection data
connection_data.Id = id
connection_data.Npid = npid
connection_data.Token = token
// Verify!
sess_string, err := environment.Env.Redis.Get("session:" + token).Result()
if err != nil {
return err
}
// Format is: ip;rank;username
sess_parts := strings.Split(sess_string, ";")
// Verify that user has the same IP as the one used to log in
if sess_parts[0] != structs.StripPort(conn.RemoteAddr().String()) {
return WrongIPAddress
}
// Get the rank id and save it in the session
connection_data.RankId, err = strconv.Atoi(sess_parts[1])
if err != nil {
return err
}
// Authenticate the session
connection_data.Authenticated = true
connection_data.Username = sess_parts[2]
// Add connection to the storage
storage.SetClientConnection(npid, connection_data)
// Return a response
err = reply.Reply(conn, packet_data.Header.Id, &protocol.AuthenticateResultMessage{
Result: proto.Int32(0),
Npid: &npid,
SessionToken: msg.Token,
})
if err != nil {
return err
}
// No idea what this thing is supposed to do
time.AfterFunc(time.Millisecond*900, func() {
err = reply.Reply(conn, 0, &protocol.AuthenticateExternalStatusMessage{
Status: proto.Int32(0),
})
if err != nil {
logger.Warning(err)
}
})
return nil
}