forked from reugn/auth-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
112 lines (90 loc) · 2.57 KB
/
server.go
File metadata and controls
112 lines (90 loc) · 2.57 KB
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
package main
import (
"fmt"
"net/http"
"strconv"
"github.com/reugn/auth-server/auth"
"github.com/reugn/auth-server/proxy"
"github.com/reugn/auth-server/repository"
"github.com/reugn/auth-server/utils"
)
// HTTPServer is an auth http server wrapper
type HTTPServer struct {
addr string
parser proxy.RequestParser
repo repository.Repository
jwtGenerator *auth.JWTGenerator
jwtValidtor *auth.JWTValidator
}
// NewHTTPServer returns a new instance of HTTPServer
func NewHTTPServer(host string, port int, keys *auth.Keys) *HTTPServer {
addr := host + ":" + strconv.Itoa(port)
repository := parseRepo()
generator := auth.NewJWTGenerator(keys, parseAlgo())
validator := auth.NewJWTValidator(keys, repository)
return &HTTPServer{
addr,
parseProxy(),
repository,
generator,
validator,
}
}
func (ws *HTTPServer) start() {
// root route
http.HandleFunc("/", rootActionHandler)
// health route
http.HandleFunc("/health", healthActionHandler)
// readiness route
http.HandleFunc("/ready", readyActionHandler)
// version route
http.HandleFunc("/version", versionActionHandler)
// token issuing route
// uses basic authentication
http.HandleFunc("/token", ws.tokenActionHandler)
// authorization route
// validates bearer JWT
http.HandleFunc("/auth", ws.authActionHandler)
err := http.ListenAndServe(ws.addr, nil)
utils.Check(err)
}
func rootActionHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
w.WriteHeader(http.StatusNotFound)
}
fmt.Fprintf(w, "")
}
func healthActionHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Ok")
}
func readyActionHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Ok")
}
func versionActionHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, authServerVersion)
}
func (ws *HTTPServer) tokenActionHandler(w http.ResponseWriter, r *http.Request) {
user, pass, ok := r.BasicAuth()
if !ok {
w.WriteHeader(http.StatusBadRequest)
return
}
userDetails := ws.repo.AuthenticateBasic(user, pass)
if userDetails == nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
token, err := ws.jwtGenerator.Generate(userDetails.UserName, userDetails.UserRole)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
fmt.Fprintf(w, "%s", token.Marshal())
}
func (ws *HTTPServer) authActionHandler(w http.ResponseWriter, r *http.Request) {
requestDetails := ws.parser.ParseRequestDetails(r)
auth := ws.parser.ParseAuthorizationToken(r)
if !ws.jwtValidtor.Authorize(auth, requestDetails) {
w.WriteHeader(http.StatusUnauthorized)
}
}