2021-03-08 16:46:23 +00:00
|
|
|
package token
|
2019-01-23 21:42:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-03-24 21:31:02 +00:00
|
|
|
"net/url"
|
2019-01-23 21:42:10 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2021-03-23 14:30:43 +00:00
|
|
|
homedir "github.com/mitchellh/go-homedir"
|
2021-03-08 16:46:23 +00:00
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflared/config"
|
2019-01-23 21:42:10 +00:00
|
|
|
)
|
|
|
|
|
2021-03-24 21:31:02 +00:00
|
|
|
// GenerateSSHCertFilePathFromURL will return a file path for creating short lived certificates
|
|
|
|
func GenerateSSHCertFilePathFromURL(url *url.URL, suffix string) (string, error) {
|
|
|
|
configPath, err := getConfigPath()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
name := strings.Replace(fmt.Sprintf("%s%s-%s", url.Hostname(), url.EscapedPath(), suffix), "/", "-", -1)
|
|
|
|
return filepath.Join(configPath, name), nil
|
|
|
|
}
|
|
|
|
|
2020-11-09 03:25:35 +00:00
|
|
|
// GenerateAppTokenFilePathFromURL will return a filepath for given Access org token
|
2021-03-10 21:52:35 +00:00
|
|
|
func GenerateAppTokenFilePathFromURL(appDomain, aud string, suffix string) (string, error) {
|
2020-11-09 03:25:35 +00:00
|
|
|
configPath, err := getConfigPath()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-03-10 21:52:35 +00:00
|
|
|
name := fmt.Sprintf("%s-%s-%s", appDomain, aud, suffix)
|
|
|
|
name = strings.Replace(strings.Replace(name, "/", "-", -1), "*", "-", -1)
|
2020-11-09 03:25:35 +00:00
|
|
|
return filepath.Join(configPath, name), nil
|
|
|
|
}
|
|
|
|
|
2021-03-08 16:46:23 +00:00
|
|
|
// generateOrgTokenFilePathFromURL will return a filepath for given Access application token
|
|
|
|
func generateOrgTokenFilePathFromURL(authDomain string) (string, error) {
|
2020-11-09 03:25:35 +00:00
|
|
|
configPath, err := getConfigPath()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
name := strings.Replace(fmt.Sprintf("%s-org-token", authDomain), "/", "-", -1)
|
|
|
|
return filepath.Join(configPath, name), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getConfigPath() (string, error) {
|
2020-08-14 20:52:47 +00:00
|
|
|
configPath, err := homedir.Expand(config.DefaultConfigSearchDirectories()[0])
|
2019-01-23 21:42:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
ok, err := config.FileExists(configPath)
|
|
|
|
if !ok && err == nil {
|
|
|
|
// create config directory if doesn't already exist
|
|
|
|
err = os.Mkdir(configPath, 0700)
|
|
|
|
}
|
2020-11-09 03:25:35 +00:00
|
|
|
return configPath, err
|
2019-01-23 21:42:10 +00:00
|
|
|
}
|