-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.go
159 lines (138 loc) · 4.17 KB
/
server.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
159
package http
import (
"fmt"
"log/slog"
"net"
"net/http"
"github.com/reugn/auth-server/internal/auth"
"github.com/reugn/auth-server/internal/config"
"github.com/reugn/auth-server/internal/proxy"
"github.com/reugn/auth-server/internal/repository"
"golang.org/x/time/rate"
)
// Server represents the entry point to interact with the service via HTTP requests.
type Server struct {
address string
version string
parser proxy.RequestParser
repository repository.Repository
rateLimiter *IPRateLimiter
ipWhiteList *IPWhiteList
jwtGenerator *auth.JWTGenerator
jwtValidator *auth.JWTValidator
}
// NewServer returns a new instance of Server.
func NewServer(version string, keys *auth.Keys, config *config.Service) (*Server, error) {
address := fmt.Sprintf("%s:%d", config.HTTP.Host, config.HTTP.Port)
repository, err := config.Repository()
if err != nil {
return nil, err
}
signingMethod, err := config.SigningMethodRSA()
if err != nil {
return nil, err
}
generator := auth.NewJWTGenerator(keys, signingMethod)
validator := auth.NewJWTValidator(keys, repository)
requestParser, err := config.RequestParser()
if err != nil {
return nil, err
}
ipWhiteList, err := NewIPWhiteList(config.HTTP.Rate.WhiteList)
if err != nil {
return nil, err
}
return &Server{
address: address,
version: version,
parser: requestParser,
repository: repository,
rateLimiter: NewIPRateLimiter(rate.Limit(config.HTTP.Rate.Tps), config.HTTP.Rate.Size),
ipWhiteList: ipWhiteList,
jwtGenerator: generator,
jwtValidator: validator,
}, nil
}
func (ws *Server) rateLimiterMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError),
http.StatusInternalServerError)
return
}
if !ws.ipWhiteList.isAllowed(ip) {
limiter := ws.rateLimiter.GetLimiter(ip)
if !limiter.Allow() {
http.Error(w, http.StatusText(http.StatusTooManyRequests),
http.StatusTooManyRequests)
return
}
}
next.ServeHTTP(w, r)
})
}
// Start initiates the HTTP server.
func (ws *Server) Start() error {
mux := http.NewServeMux()
// root route
mux.HandleFunc("/", rootActionHandler)
// health route
mux.HandleFunc("/health", healthActionHandler)
// readiness route
mux.HandleFunc("/ready", readyActionHandler)
// version route
mux.HandleFunc("/version", ws.versionActionHandler)
// token issuing route, requires basic authentication
mux.HandleFunc("/token", ws.tokenActionHandler)
// authorization route, requires a JSON Web Token
mux.HandleFunc("/auth", ws.authActionHandler)
return http.ListenAndServe(ws.address, ws.rateLimiterMiddleware(mux))
}
func rootActionHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
w.WriteHeader(http.StatusNotFound)
}
fmt.Fprintf(w, "")
}
func healthActionHandler(w http.ResponseWriter, _ *http.Request) {
fmt.Fprintf(w, "Ok")
}
func readyActionHandler(w http.ResponseWriter, _ *http.Request) {
fmt.Fprintf(w, "Ok")
}
func (ws *Server) versionActionHandler(w http.ResponseWriter, _ *http.Request) {
fmt.Fprint(w, ws.version)
}
func (ws *Server) tokenActionHandler(w http.ResponseWriter, r *http.Request) {
slog.Debug("Token generation request")
user, pass, ok := r.BasicAuth()
if !ok {
w.WriteHeader(http.StatusBadRequest)
return
}
userDetails := ws.repository.AuthenticateBasic(user, pass)
if userDetails == nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
accessToken, err := ws.jwtGenerator.Generate(userDetails.UserName, userDetails.UserRole)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
marshalled, err := accessToken.Marshal()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
fmt.Fprintf(w, "%s", marshalled)
}
func (ws *Server) authActionHandler(w http.ResponseWriter, r *http.Request) {
slog.Debug("Token authorization request")
requestDetails := ws.parser.ParseRequestDetails(r)
authToken := ws.parser.ParseAuthorizationToken(r)
if !ws.jwtValidator.Authorize(authToken, requestDetails) {
w.WriteHeader(http.StatusUnauthorized)
}
}