mirror of
https://github.com/mickael-kerjean/filestash
synced 2025-12-06 16:32:31 +01:00
feature (template): additional template vars
This commit is contained in:
parent
e7cd78b164
commit
c9bfdb51c4
2 changed files with 50 additions and 29 deletions
|
|
@ -1,17 +1,14 @@
|
||||||
package ctrl
|
package ctrl
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
. "github.com/mickael-kerjean/filestash/server/common"
|
. "github.com/mickael-kerjean/filestash/server/common"
|
||||||
|
|
@ -270,7 +267,7 @@ func SessionAuthMiddleware(ctx *App, res http.ResponseWriter, req *http.Request)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
idpParams := map[string]string{}
|
idpParams := TmplParams(map[string]string{})
|
||||||
if err := json.Unmarshal(
|
if err := json.Unmarshal(
|
||||||
[]byte(Config.Get("middleware.identity_provider.params").String()),
|
[]byte(Config.Get("middleware.identity_provider.params").String()),
|
||||||
&idpParams,
|
&idpParams,
|
||||||
|
|
@ -282,6 +279,18 @@ func SessionAuthMiddleware(ctx *App, res http.ResponseWriter, req *http.Request)
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
for k, v := range idpParams {
|
||||||
|
out, err := TmplExec(NewStringFromInterface(v), idpParams)
|
||||||
|
if err != nil {
|
||||||
|
http.Redirect(
|
||||||
|
res, req,
|
||||||
|
"/?error=Not%20Valid&trace=idp - "+err.Error(),
|
||||||
|
http.StatusTemporaryRedirect,
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
idpParams[k] = out
|
||||||
|
}
|
||||||
|
|
||||||
// Step1: Entrypoint of the authentication process is handled by the plugin
|
// Step1: Entrypoint of the authentication process is handled by the plugin
|
||||||
if req.Method == "GET" && _get.Get("action") == "redirect" {
|
if req.Method == "GET" && _get.Get("action") == "redirect" {
|
||||||
|
|
@ -331,14 +340,7 @@ func SessionAuthMiddleware(ctx *App, res http.ResponseWriter, req *http.Request)
|
||||||
} else if err != nil { // response handled directly within a plugin
|
} else if err != nil { // response handled directly within a plugin
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
templateBind = TmplParams(templateBind)
|
||||||
templateBind["machine_id"] = GenerateMachineID()
|
|
||||||
for _, value := range os.Environ() {
|
|
||||||
pair := strings.SplitN(value, "=", 2)
|
|
||||||
if len(pair) == 2 {
|
|
||||||
templateBind[fmt.Sprintf("ENV_%s", pair[0])] = pair[1]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
label = ""
|
label = ""
|
||||||
|
|
@ -417,25 +419,11 @@ func SessionAuthMiddleware(ctx *App, res http.ResponseWriter, req *http.Request)
|
||||||
}
|
}
|
||||||
mappingToUse := map[string]string{}
|
mappingToUse := map[string]string{}
|
||||||
for k, v := range globalMapping[label] {
|
for k, v := range globalMapping[label] {
|
||||||
str := NewStringFromInterface(v)
|
out, err := TmplExec(NewStringFromInterface(v), tb)
|
||||||
if str == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
tmpl, err := template.
|
|
||||||
New("ctrl::session::auth_middleware").
|
|
||||||
Funcs(tmplFuncs).
|
|
||||||
Parse(str)
|
|
||||||
mappingToUse[k] = str
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Log.Debug("session::authMiddleware 'template creation failed %s'", err.Error())
|
Log.Debug("session::authMiddleware action=tmplExec err=%s", err.Error())
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
var b bytes.Buffer
|
mappingToUse[k] = out
|
||||||
if err = tmpl.Execute(&b, tb); err != nil {
|
|
||||||
Log.Debug("session::authMiddleware 'template execution failed %s'", err.Error())
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
mappingToUse[k] = b.String()
|
|
||||||
}
|
}
|
||||||
mappingToUse["timestamp"] = time.Now().Format(time.RFC3339)
|
mappingToUse["timestamp"] = time.Now().Format(time.RFC3339)
|
||||||
return mappingToUse, nil
|
return mappingToUse, nil
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,13 @@
|
||||||
package ctrl
|
package ctrl
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"crypto/rsa"
|
"crypto/rsa"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
@ -15,6 +17,37 @@ import (
|
||||||
"github.com/golang-jwt/jwt/v5"
|
"github.com/golang-jwt/jwt/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TmplExec(params string, input map[string]string) (string, error) {
|
||||||
|
if params == "" {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
tmpl, err := template.
|
||||||
|
New("ctrl::session::auth_middleware").
|
||||||
|
Funcs(tmplFuncs).
|
||||||
|
Parse(params)
|
||||||
|
if err != nil {
|
||||||
|
Log.Debug("tmpl::execute action=parse err=%s", err.Error())
|
||||||
|
return params, err
|
||||||
|
}
|
||||||
|
var b bytes.Buffer
|
||||||
|
if err = tmpl.Execute(&b, input); err != nil {
|
||||||
|
Log.Debug("tmpl::execute action=execute err%s", err.Error())
|
||||||
|
return params, err
|
||||||
|
}
|
||||||
|
return b.String(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func TmplParams(data map[string]string) map[string]string {
|
||||||
|
data["machine_id"] = GenerateMachineID()
|
||||||
|
for _, value := range os.Environ() {
|
||||||
|
pair := strings.SplitN(value, "=", 2)
|
||||||
|
if len(pair) == 2 {
|
||||||
|
data[fmt.Sprintf("ENV_%s", pair[0])] = pair[1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
var tmplFuncs = template.FuncMap{
|
var tmplFuncs = template.FuncMap{
|
||||||
"split": func(s, sep string) []string {
|
"split": func(s, sep string) []string {
|
||||||
return strings.Split(sep, s)
|
return strings.Split(sep, s)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue