Mercurial > gemma
annotate cmd/tokenserver/main.go @ 127:44794c641277
Implemented explicit token renewal under endpoint /api/renew.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Thu, 28 Jun 2018 13:39:14 +0200 |
parents | 89cf2e7672ff |
children | 441a8ee637c5 |
rev | line source |
---|---|
1
0e1d0c00bc74
Useless webserver to test go-gettablity.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
1 package main |
0e1d0c00bc74
Useless webserver to test go-gettablity.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
2 |
0e1d0c00bc74
Useless webserver to test go-gettablity.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
3 import ( |
0e1d0c00bc74
Useless webserver to test go-gettablity.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
4 "flag" |
0e1d0c00bc74
Useless webserver to test go-gettablity.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
5 "fmt" |
0e1d0c00bc74
Useless webserver to test go-gettablity.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
6 "log" |
0e1d0c00bc74
Useless webserver to test go-gettablity.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
7 "net/http" |
3 | 8 "path/filepath" |
2
9c6f68a8e8b2
Demo generation of tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1
diff
changeset
|
9 |
125
a98a282f00e1
Wired token generator and connection pool to token server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
15
diff
changeset
|
10 "gemma.intevation.de/gemma/auth" |
1
0e1d0c00bc74
Useless webserver to test go-gettablity.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
11 ) |
0e1d0c00bc74
Useless webserver to test go-gettablity.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
12 |
127
44794c641277
Implemented explicit token renewal under endpoint /api/renew.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
126
diff
changeset
|
13 func renew(rw http.ResponseWriter, req *http.Request) { |
44794c641277
Implemented explicit token renewal under endpoint /api/renew.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
126
diff
changeset
|
14 token, _ := auth.GetToken(req) |
44794c641277
Implemented explicit token renewal under endpoint /api/renew.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
126
diff
changeset
|
15 newToken, err := auth.ConnPool.Replace(token, auth.GenerateToken) |
44794c641277
Implemented explicit token renewal under endpoint /api/renew.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
126
diff
changeset
|
16 switch { |
44794c641277
Implemented explicit token renewal under endpoint /api/renew.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
126
diff
changeset
|
17 case err == auth.ErrNoSuchToken: |
44794c641277
Implemented explicit token renewal under endpoint /api/renew.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
126
diff
changeset
|
18 http.NotFound(rw, req) |
44794c641277
Implemented explicit token renewal under endpoint /api/renew.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
126
diff
changeset
|
19 return |
44794c641277
Implemented explicit token renewal under endpoint /api/renew.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
126
diff
changeset
|
20 case err != nil: |
44794c641277
Implemented explicit token renewal under endpoint /api/renew.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
126
diff
changeset
|
21 http.Error(rw, fmt.Sprintf("error: %v", err), http.StatusInternalServerError) |
44794c641277
Implemented explicit token renewal under endpoint /api/renew.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
126
diff
changeset
|
22 return |
44794c641277
Implemented explicit token renewal under endpoint /api/renew.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
126
diff
changeset
|
23 } |
44794c641277
Implemented explicit token renewal under endpoint /api/renew.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
126
diff
changeset
|
24 rw.Header().Set("Content-Type", "text/plain") |
44794c641277
Implemented explicit token renewal under endpoint /api/renew.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
126
diff
changeset
|
25 fmt.Fprintf(rw, "%s\n", newToken) |
44794c641277
Implemented explicit token renewal under endpoint /api/renew.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
126
diff
changeset
|
26 } |
44794c641277
Implemented explicit token renewal under endpoint /api/renew.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
126
diff
changeset
|
27 |
126
89cf2e7672ff
Implemented an explicit token deletion under endpoint /api/logout.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
125
diff
changeset
|
28 func logout(rw http.ResponseWriter, req *http.Request) { |
89cf2e7672ff
Implemented an explicit token deletion under endpoint /api/logout.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
125
diff
changeset
|
29 token, _ := auth.GetToken(req) |
89cf2e7672ff
Implemented an explicit token deletion under endpoint /api/logout.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
125
diff
changeset
|
30 deleted := auth.ConnPool.Delete(token) |
89cf2e7672ff
Implemented an explicit token deletion under endpoint /api/logout.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
125
diff
changeset
|
31 if !deleted { |
89cf2e7672ff
Implemented an explicit token deletion under endpoint /api/logout.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
125
diff
changeset
|
32 http.NotFound(rw, req) |
127
44794c641277
Implemented explicit token renewal under endpoint /api/renew.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
126
diff
changeset
|
33 return |
126
89cf2e7672ff
Implemented an explicit token deletion under endpoint /api/logout.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
125
diff
changeset
|
34 } |
89cf2e7672ff
Implemented an explicit token deletion under endpoint /api/logout.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
125
diff
changeset
|
35 rw.Header().Set("Content-Type", "text/plain") |
89cf2e7672ff
Implemented an explicit token deletion under endpoint /api/logout.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
125
diff
changeset
|
36 fmt.Fprintln(rw, "token deleted") |
89cf2e7672ff
Implemented an explicit token deletion under endpoint /api/logout.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
125
diff
changeset
|
37 } |
89cf2e7672ff
Implemented an explicit token deletion under endpoint /api/logout.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
125
diff
changeset
|
38 |
2
9c6f68a8e8b2
Demo generation of tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1
diff
changeset
|
39 func token(rw http.ResponseWriter, req *http.Request) { |
9c6f68a8e8b2
Demo generation of tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1
diff
changeset
|
40 user := req.FormValue("user") |
9c6f68a8e8b2
Demo generation of tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1
diff
changeset
|
41 password := req.FormValue("password") |
9c6f68a8e8b2
Demo generation of tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1
diff
changeset
|
42 |
125
a98a282f00e1
Wired token generator and connection pool to token server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
15
diff
changeset
|
43 token, err := auth.GenerateToken(user, password) |
2
9c6f68a8e8b2
Demo generation of tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1
diff
changeset
|
44 |
9c6f68a8e8b2
Demo generation of tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1
diff
changeset
|
45 if err != nil { |
125
a98a282f00e1
Wired token generator and connection pool to token server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
15
diff
changeset
|
46 http.Error(rw, fmt.Sprintf("error: %v", err), http.StatusInternalServerError) |
2
9c6f68a8e8b2
Demo generation of tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1
diff
changeset
|
47 return |
9c6f68a8e8b2
Demo generation of tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1
diff
changeset
|
48 } |
9c6f68a8e8b2
Demo generation of tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1
diff
changeset
|
49 |
9c6f68a8e8b2
Demo generation of tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1
diff
changeset
|
50 rw.Header().Set("Content-Type", "text/plain") |
125
a98a282f00e1
Wired token generator and connection pool to token server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
15
diff
changeset
|
51 fmt.Fprintf(rw, "%s\n", token) |
2
9c6f68a8e8b2
Demo generation of tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1
diff
changeset
|
52 } |
9c6f68a8e8b2
Demo generation of tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1
diff
changeset
|
53 |
1
0e1d0c00bc74
Useless webserver to test go-gettablity.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
54 func main() { |
3 | 55 port := flag.Int("port", 8000, "port to listen at.") |
1
0e1d0c00bc74
Useless webserver to test go-gettablity.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
56 host := flag.String("host", "localhost", "host to listen at.") |
0e1d0c00bc74
Useless webserver to test go-gettablity.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
57 flag.Parse() |
3 | 58 p, _ := filepath.Abs("./web") |
1
0e1d0c00bc74
Useless webserver to test go-gettablity.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
59 mux := http.NewServeMux() |
3 | 60 mux.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir(p)))) |
15
05d828374256
Reverted to previous setup of /api prefixing only the token route
Thomas Junk <thomas.junk@intevation.de>
parents:
14
diff
changeset
|
61 mux.HandleFunc("/api/token", token) |
126
89cf2e7672ff
Implemented an explicit token deletion under endpoint /api/logout.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
125
diff
changeset
|
62 mux.Handle("/api/logout", auth.JWTMiddleware(http.HandlerFunc(token))) |
1
0e1d0c00bc74
Useless webserver to test go-gettablity.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
63 |
0e1d0c00bc74
Useless webserver to test go-gettablity.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
64 addr := fmt.Sprintf("%s:%d", *host, *port) |
15
05d828374256
Reverted to previous setup of /api prefixing only the token route
Thomas Junk <thomas.junk@intevation.de>
parents:
14
diff
changeset
|
65 log.Fatalln(http.ListenAndServe(addr, mux)) |
1
0e1d0c00bc74
Useless webserver to test go-gettablity.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
66 } |