forked from okzk/oidc-radius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
139 lines (127 loc) · 3.75 KB
/
main.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
package main
import (
"crypto/sha256"
"encoding/base64"
"fmt"
"github.com/go-redis/redis"
"github.com/okzk/go-ciba"
"layeh.com/radius"
"layeh.com/radius/rfc2865"
"log"
"os"
"strconv"
"strings"
"time"
)
func genCacheKey(keys ...string) string {
h := sha256.New()
for _, k := range keys {
h.Write([]byte(k))
h.Write([]byte("\n@@@@@@@@@\n"))
}
return base64.RawURLEncoding.EncodeToString(h.Sum(nil))
}
func main() {
client := ciba.NewClient(
os.Getenv("CIBA_ISSUER"),
os.Getenv("CIBA_AUTHN_ENDPOINT"),
os.Getenv("CIBA_TOKEN_ENDPOINT"),
os.Getenv("CIBA_SCOPE"),
os.Getenv("CIBA_CLIENT_ID"),
os.Getenv("CIBA_CLIENT_SECRET"),
)
sep := os.Getenv("USERNAME_SEPARATOR")
acceptCaseInsensitiveUsername := os.Getenv("ACCEPT_CASE_INSENSITIVE_USERNAME") == "1"
expiration := 8 * time.Hour
expirationStr := os.Getenv("CACHE_EXPIRATION_SEC")
if expirationStr != "" {
s, err := strconv.Atoi(expirationStr)
if err != nil || s <= 0 {
panic("invalid CACHE_EXPIRATION_SEC env")
}
expiration = time.Duration(s) * time.Second
}
redisClient := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%s", os.Getenv("REDIS_HOST"), os.Getenv("REDIS_PORT")),
Password: os.Getenv("REDIS_PASSWORD"),
DialTimeout: time.Millisecond * 300,
ReadTimeout: time.Millisecond * 300,
})
accessRequestHandler := func(w radius.ResponseWriter, r *radius.Request) {
if r.Code != radius.CodeAccessRequest {
return
}
username := rfc2865.UserName_GetString(r.Packet)
password := ""
if sep != "" {
parts := strings.SplitN(username, sep, 2)
if len(parts) == 2 {
username = parts[0]
password = parts[1]
}
}
if password == "" {
password = rfc2865.UserPassword_GetString(r.Packet)
}
if username == "" || password == "" {
w.Write(r.Response(radius.CodeAccessReject))
return
}
cacheKey := genCacheKey(username, password)
ret, _ := redisClient.Get(cacheKey).Result()
if ret == username {
log.Printf("[INFO] (cache) authn success. user: %s", username)
w.Write(r.Response(radius.CodeAccessAccept))
return
}
token, err := client.Authenticate(r.Context(), ciba.LoginHint(username), ciba.UserCode(password))
if err != nil {
log.Printf("[INFO] authn failed. user: %s, error: %v", username, err)
w.Write(r.Response(radius.CodeAccessReject))
return
}
sub, ok := token.Claims()["sub"].(string)
if !ok {
log.Printf("[INFO] authn failed. user: %s, error: missing_sub", username)
w.Write(r.Response(radius.CodeAccessReject))
return
}
if acceptCaseInsensitiveUsername && !strings.EqualFold(sub, username) {
log.Printf("[INFO] authn failed. user: %s, returned_sub: %s", username, sub)
w.Write(r.Response(radius.CodeAccessReject))
return
}
if !acceptCaseInsensitiveUsername && sub != username {
log.Printf("[INFO] authn failed. user: %s, returned_sub: %s", username, sub)
w.Write(r.Response(radius.CodeAccessReject))
return
}
log.Printf("[INFO] authn success. user: %s", username)
redisClient.Set(cacheKey, username, expiration)
w.Write(r.Response(radius.CodeAccessAccept))
}
accountingRequestHandler := func(w radius.ResponseWriter, r *radius.Request) {
if r.Code != radius.CodeAccountingRequest {
return
}
w.Write(r.Response(radius.CodeAccountingResponse))
}
secret := []byte(os.Getenv("RADIUS_SECRET"))
go func() {
server := radius.PacketServer{
Addr: ":1813",
Handler: radius.HandlerFunc(accountingRequestHandler),
SecretSource: radius.StaticSecretSource(secret),
}
if err := server.ListenAndServe(); err != nil {
panic(err)
}
}()
server := radius.PacketServer{
Handler: radius.HandlerFunc(accessRequestHandler),
SecretSource: radius.StaticSecretSource(secret),
}
if err := server.ListenAndServe(); err != nil {
panic(err)
}
}