Mercurial > gemma
annotate cmd/tokenserver/main.go @ 139:6eeaf9682c78
Fixed interpreter in third party library downloader.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Sat, 30 Jun 2018 23:23:08 +0200 |
parents | 441a8ee637c5 |
children | 0c56c56a1c44 |
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 |
128
441a8ee637c5
Added claims checker + example.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
127
diff
changeset
|
13 func sysAdmin(rw http.ResponseWriter, req *http.Request) { |
441a8ee637c5
Added claims checker + example.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
127
diff
changeset
|
14 claims, _ := auth.GetClaims(req) |
441a8ee637c5
Added claims checker + example.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
127
diff
changeset
|
15 rw.Header().Set("Content-Type", "text/plain") |
441a8ee637c5
Added claims checker + example.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
127
diff
changeset
|
16 fmt.Fprintf(rw, "%s is a sys_admin\n", claims.User) |
441a8ee637c5
Added claims checker + example.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
127
diff
changeset
|
17 } |
441a8ee637c5
Added claims checker + example.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
127
diff
changeset
|
18 |
127
44794c641277
Implemented explicit token renewal under endpoint /api/renew.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
126
diff
changeset
|
19 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
|
20 token, _ := auth.GetToken(req) |
44794c641277
Implemented explicit token renewal under endpoint /api/renew.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
126
diff
changeset
|
21 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
|
22 switch { |
44794c641277
Implemented explicit token renewal under endpoint /api/renew.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
126
diff
changeset
|
23 case err == auth.ErrNoSuchToken: |
44794c641277
Implemented explicit token renewal under endpoint /api/renew.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
126
diff
changeset
|
24 http.NotFound(rw, req) |
44794c641277
Implemented explicit token renewal under endpoint /api/renew.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
126
diff
changeset
|
25 return |
44794c641277
Implemented explicit token renewal under endpoint /api/renew.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
126
diff
changeset
|
26 case err != nil: |
44794c641277
Implemented explicit token renewal under endpoint /api/renew.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
126
diff
changeset
|
27 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
|
28 return |
44794c641277
Implemented explicit token renewal under endpoint /api/renew.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
126
diff
changeset
|
29 } |
44794c641277
Implemented explicit token renewal under endpoint /api/renew.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
126
diff
changeset
|
30 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
|
31 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
|
32 } |
44794c641277
Implemented explicit token renewal under endpoint /api/renew.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
126
diff
changeset
|
33 |
126
89cf2e7672ff
Implemented an explicit token deletion under endpoint /api/logout.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
125
diff
changeset
|
34 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
|
35 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
|
36 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
|
37 if !deleted { |
89cf2e7672ff
Implemented an explicit token deletion under endpoint /api/logout.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
125
diff
changeset
|
38 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
|
39 return |
126
89cf2e7672ff
Implemented an explicit token deletion under endpoint /api/logout.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
125
diff
changeset
|
40 } |
89cf2e7672ff
Implemented an explicit token deletion under endpoint /api/logout.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
125
diff
changeset
|
41 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
|
42 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
|
43 } |
89cf2e7672ff
Implemented an explicit token deletion under endpoint /api/logout.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
125
diff
changeset
|
44 |
2
9c6f68a8e8b2
Demo generation of tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1
diff
changeset
|
45 func token(rw http.ResponseWriter, req *http.Request) { |
9c6f68a8e8b2
Demo generation of tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1
diff
changeset
|
46 user := req.FormValue("user") |
9c6f68a8e8b2
Demo generation of tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1
diff
changeset
|
47 password := req.FormValue("password") |
9c6f68a8e8b2
Demo generation of tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1
diff
changeset
|
48 |
125
a98a282f00e1
Wired token generator and connection pool to token server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
15
diff
changeset
|
49 token, err := auth.GenerateToken(user, password) |
2
9c6f68a8e8b2
Demo generation of tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1
diff
changeset
|
50 |
9c6f68a8e8b2
Demo generation of tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1
diff
changeset
|
51 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
|
52 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
|
53 return |
9c6f68a8e8b2
Demo generation of tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1
diff
changeset
|
54 } |
9c6f68a8e8b2
Demo generation of tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1
diff
changeset
|
55 |
9c6f68a8e8b2
Demo generation of tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1
diff
changeset
|
56 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
|
57 fmt.Fprintf(rw, "%s\n", token) |
2
9c6f68a8e8b2
Demo generation of tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1
diff
changeset
|
58 } |
9c6f68a8e8b2
Demo generation of tokens.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1
diff
changeset
|
59 |
1
0e1d0c00bc74
Useless webserver to test go-gettablity.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
60 func main() { |
3 | 61 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
|
62 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
|
63 flag.Parse() |
3 | 64 p, _ := filepath.Abs("./web") |
1
0e1d0c00bc74
Useless webserver to test go-gettablity.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
65 mux := http.NewServeMux() |
3 | 66 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
|
67 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
|
68 mux.Handle("/api/logout", auth.JWTMiddleware(http.HandlerFunc(token))) |
128
441a8ee637c5
Added claims checker + example.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
127
diff
changeset
|
69 mux.Handle("/api/renew", auth.JWTMiddleware(http.HandlerFunc(renew))) |
441a8ee637c5
Added claims checker + example.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
127
diff
changeset
|
70 mux.Handle("/api/sys_admin", |
441a8ee637c5
Added claims checker + example.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
127
diff
changeset
|
71 auth.JWTMiddleware( |
441a8ee637c5
Added claims checker + example.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
127
diff
changeset
|
72 auth.ClaimsChecker(http.HandlerFunc(sysAdmin), auth.HasRole("sys_admin")))) |
1
0e1d0c00bc74
Useless webserver to test go-gettablity.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
73 |
0e1d0c00bc74
Useless webserver to test go-gettablity.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
74 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
|
75 log.Fatalln(http.ListenAndServe(addr, mux)) |
1
0e1d0c00bc74
Useless webserver to test go-gettablity.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
76 } |