Last active
August 6, 2023 01:31
-
-
Save winebarrel/fc640578525ac9f4619082be61b9b880 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/base64" | |
"fmt" | |
"log" | |
"net/http" | |
"net/url" | |
"os" | |
"strings" | |
"github.com/gin-gonic/gin" | |
) | |
const ( | |
ParentAccountID = "123456789012" | |
StartURLPrefix = "https://d-12345abcde.awsapps.com/start/#/saml/custom" | |
) | |
type Account struct { | |
ID string | |
InsID string | |
PIDByRole map[string]string | |
} | |
var AccountByName map[string]Account = map[string]Account{ | |
"my-account": { | |
ID: "123456789012", | |
InsID: "ins-1234567890abcedf", | |
PIDByRole: map[string]string{ | |
"Admin": "p-9994567890ab", | |
"PowerUser": "p-1234567890ab", | |
}, | |
}, | |
"my-alt-account-xxx": { | |
ID: "999456789012", | |
InsID: "ins-abcedf7890abcedf", | |
PIDByRole: map[string]string{ | |
"DBOperator": "p-abcdef7890ab", | |
}, | |
}, | |
} | |
func main() { | |
r := gin.Default() | |
r.GET("/:account/:role", func(c *gin.Context) { | |
accountName := c.Param("account") | |
roleName := c.Param("role") | |
account, ok := AccountByName[accountName] | |
if !ok { | |
c.Status(http.StatusNotFound) | |
return | |
} | |
pid, ok := account.PIDByRole[roleName] | |
if !ok { | |
c.Status(http.StatusNotFound) | |
return | |
} | |
_1stPath := fmt.Sprintf("%s (%s)", account.ID, accountName) | |
_1stPath = url.PathEscape(_1stPath) | |
_2ndPath := strings.Join([]string{ParentAccountID, account.InsID, pid}, "_") | |
_2ndPath = base64.URLEncoding.EncodeToString([]byte(_2ndPath)) | |
_2ndPath = url.PathEscape(_2ndPath) | |
url := strings.Join([]string{StartURLPrefix, _1stPath, _2ndPath}, "/") | |
c.Redirect(http.StatusFound, url) | |
}) | |
addr := os.Getenv("ADDR") | |
port := os.Getenv("PORT") | |
if addr == "" { | |
addr = "127.0.0.1" | |
} | |
if port == "" { | |
port = "8080" | |
} | |
err := r.Run(fmt.Sprintf("%s:%s", addr, port)) | |
if err != nil { | |
log.Fatal(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment