twitch-bot/internal/template/strings/strings.go
Knut Ahlers a39dc5e4c6
[templating] Add b64urldec and b64urlenc functions
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2023-08-25 13:14:23 +02:00

27 lines
683 B
Go

package strings
import (
"encoding/base64"
"github.com/pkg/errors"
"github.com/Luzifer/twitch-bot/v3/plugins"
)
func Register(args plugins.RegistrationArguments) error {
args.RegisterTemplateFunction("b64urlenc", plugins.GenericTemplateFunctionGetter(base64URLEncode))
args.RegisterTemplateFunction("b64urldec", plugins.GenericTemplateFunctionGetter(base64URLDecode))
return nil
}
func base64URLEncode(v string) string {
return base64.URLEncoding.EncodeToString([]byte(v))
}
func base64URLDecode(v string) (string, error) {
data, err := base64.URLEncoding.DecodeString(v)
if err != nil {
return "", errors.Wrap(err, "decoding string")
}
return string(data), nil
}