github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/docs/iam/identity-manager-plugin.go (about)

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