-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathauth.go
158 lines (136 loc) · 2.92 KB
/
auth.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package dbus
import(
"strings"
"container/list"
"fmt"
"os"
"net"
)
var(
ErrAuthUnknownCommand = os.NewError("UnknowAuthCommand")
ErrAuthFailed = os.NewError("AuthenticationFailed")
)
type Authenticator interface{
Mechanism() string;
Authenticate() string;
}
type AuthExternal struct{
}
func(p *AuthExternal) Mechanism() string{ return "EXTERNAL"}
func(p *AuthExternal) Authenticate() string{
return fmt.Sprintf("%x", fmt.Sprintf("%d", os.Getuid()))
}
type authStatus int
const (
STARTING = iota
WAITING_FOR_DATA
WAITING_FOR_OK
WAITING_FOR_REJECT
AUTH_CONTINUE
AUTH_OK
AUTH_ERROR
AUTHENTICATED
AUTH_NEXT
)
type authState struct{
status authStatus
auth Authenticator
authList list.List
conn net.Conn
}
func(p *authState) AddAuthenticator(auth Authenticator){
p.authList.PushBack(auth)
}
func(p *authState) _NextAuthenticator(){
if p.authList.Len() == 0{
p.auth = nil
return
}
p.auth,_ = p.authList.Front().Value.(Authenticator)
p.authList.Remove(p.authList.Front())
msg := strings.Join([]string{"AUTH", p.auth.Mechanism(), p.auth.Authenticate()}, " ")
p._Send(msg)
}
func(p *authState) _NextMessage() []string{
b := make([]byte, 4096)
p.conn.Read(b)
retstr := string(b)
return strings.Split(strings.TrimSpace(retstr), " ", 0)
}
func(p *authState) _Send(msg string){
p.conn.Write(strings.Bytes(msg + "\r\n"));
}
func(p *authState) Authenticate(conn net.Conn) os.Error{
p.conn = conn
p.conn.Write(strings.Bytes("\x00"))
p._NextAuthenticator()
p.status = STARTING
for ;p.status != AUTHENTICATED;{
if nil == p.auth { return ErrAuthFailed}
if err := p._NextState(); err != nil{ return err}
}
return nil
}
func(p *authState) _NextState() (err os.Error){
nextMsg := p._NextMessage()
if STARTING == p.status {
switch nextMsg[0]{
case "CONTINUE":
p.status = WAITING_FOR_DATA
case "OK":
p.status = WAITING_FOR_OK
}
}
switch p.status{
case WAITING_FOR_DATA:
err = p._WaitingForData(nextMsg)
case WAITING_FOR_OK:
err = p._WaitingForOK(nextMsg)
case WAITING_FOR_REJECT:
err = p._WaitingForReject(nextMsg)
}
return;
}
func(p *authState) _WaitingForData(msg []string) os.Error{
switch msg[0]{
case "DATA":
return ErrAuthUnknownCommand
case "REJECTED":
p._NextAuthenticator()
p.status = WAITING_FOR_DATA
case "OK":
p._Send("BEGIN")
p.status = AUTHENTICATED
default:
p._Send("ERROR")
p.status = WAITING_FOR_DATA
}
return nil
}
func(p *authState) _WaitingForOK(msg []string) os.Error{
switch msg[0]{
case "OK":
p._Send("BEGIN")
p.status = AUTHENTICATED
case "REJECT":
p._NextAuthenticator()
p.status = WAITING_FOR_DATA
case "DATA", "ERROR":
p._Send("CANCEL")
p.status = WAITING_FOR_REJECT
default:
p._Send("ERROR")
p.status = WAITING_FOR_OK
}
return nil
}
func(p *authState) _WaitingForReject(msg []string) os.Error{
switch msg[0]{
case "REJECT":
p._NextAuthenticator()
p.status = WAITING_FOR_OK
default:
return ErrAuthUnknownCommand
}
return nil
}