You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.1 KiB

  1. //go:build ignore
  2. // +build ignore
  3. // Copyright (c) 2015-2022 MinIO, Inc.
  4. //
  5. // This file is part of MinIO Object Storage stack
  6. //
  7. // This program is free software: you can redistribute it and/or modify
  8. // it under the terms of the GNU Affero General Public License as published by
  9. // the Free Software Foundation, either version 3 of the License, or
  10. // (at your option) any later version.
  11. //
  12. // This program is distributed in the hope that it will be useful
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU Affero General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU Affero General Public License
  18. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. package main
  20. import (
  21. "encoding/json"
  22. "errors"
  23. "fmt"
  24. "log"
  25. "net/http"
  26. )
  27. func writeErrorResponse(w http.ResponseWriter, err error) {
  28. w.WriteHeader(http.StatusBadRequest)
  29. json.NewEncoder(w).Encode(map[string]string{
  30. "reason": fmt.Sprintf("%v", err),
  31. })
  32. }
  33. type Resp struct {
  34. User string `json:"user"`
  35. MaxValiditySeconds int `json:"maxValiditySeconds"`
  36. Claims map[string]interface{} `json:"claims"`
  37. }
  38. var tokens map[string]Resp = map[string]Resp{
  39. "aaa": {
  40. User: "Alice",
  41. MaxValiditySeconds: 3600,
  42. Claims: map[string]interface{}{
  43. "groups": []string{"data-science"},
  44. },
  45. },
  46. "bbb": {
  47. User: "Bart",
  48. MaxValiditySeconds: 3600,
  49. Claims: map[string]interface{}{
  50. "groups": []string{"databases"},
  51. },
  52. },
  53. }
  54. func mainHandler(w http.ResponseWriter, r *http.Request) {
  55. token := r.FormValue("token")
  56. if token == "" {
  57. writeErrorResponse(w, errors.New("token parameter not given"))
  58. return
  59. }
  60. rsp, ok := tokens[token]
  61. if !ok {
  62. w.WriteHeader(http.StatusForbidden)
  63. return
  64. }
  65. fmt.Printf("Allowed for token: %s user: %s\n", token, rsp.User)
  66. w.WriteHeader(http.StatusOK)
  67. json.NewEncoder(w).Encode(rsp)
  68. return
  69. }
  70. func main() {
  71. http.HandleFunc("/", mainHandler)
  72. log.Print("Listing on :8081")
  73. log.Fatal(http.ListenAndServe(":8081", nil))
  74. }