2018-10-19 20:44:35 +00:00
|
|
|
package token
|
2018-10-08 19:20:28 +00:00
|
|
|
|
|
|
|
import (
|
2019-06-26 15:48:45 +00:00
|
|
|
"context"
|
2020-02-24 02:48:06 +00:00
|
|
|
"encoding/json"
|
2019-06-26 15:48:45 +00:00
|
|
|
"fmt"
|
2018-10-08 19:20:28 +00:00
|
|
|
"io/ioutil"
|
2020-11-09 03:25:35 +00:00
|
|
|
"net/http"
|
2018-10-08 19:20:28 +00:00
|
|
|
"net/url"
|
2019-06-26 15:48:45 +00:00
|
|
|
"os"
|
2019-07-31 20:24:57 +00:00
|
|
|
"os/signal"
|
2020-11-09 03:25:35 +00:00
|
|
|
"strings"
|
2019-07-31 20:24:57 +00:00
|
|
|
"syscall"
|
2020-02-24 02:48:06 +00:00
|
|
|
"time"
|
2018-10-08 19:20:28 +00:00
|
|
|
|
2020-11-09 03:25:35 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-11-25 06:55:13 +00:00
|
|
|
"github.com/rs/zerolog"
|
2022-03-18 17:11:44 +00:00
|
|
|
"gopkg.in/square/go-jose.v2"
|
2021-03-23 14:30:43 +00:00
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflared/config"
|
2021-03-26 04:04:56 +00:00
|
|
|
"github.com/cloudflare/cloudflared/retry"
|
2019-01-23 21:42:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-03-10 21:52:35 +00:00
|
|
|
keyName = "token"
|
|
|
|
tokenCookie = "CF_Authorization"
|
|
|
|
appDomainHeader = "CF-Access-Domain"
|
2021-04-20 17:04:00 +00:00
|
|
|
appAUDHeader = "CF-Access-Aud"
|
2021-03-10 21:52:35 +00:00
|
|
|
AccessLoginWorkerPath = "/cdn-cgi/access/login"
|
2018-10-08 19:20:28 +00:00
|
|
|
)
|
|
|
|
|
2022-06-24 18:51:53 +00:00
|
|
|
var (
|
|
|
|
userAgent = "DEV"
|
|
|
|
)
|
|
|
|
|
2021-03-10 21:52:35 +00:00
|
|
|
type AppInfo struct {
|
|
|
|
AuthDomain string
|
|
|
|
AppAUD string
|
|
|
|
AppDomain string
|
|
|
|
}
|
|
|
|
|
2019-06-26 15:48:45 +00:00
|
|
|
type lock struct {
|
|
|
|
lockFilePath string
|
2021-03-26 04:04:56 +00:00
|
|
|
backoff *retry.BackoffHandler
|
2019-11-12 18:50:41 +00:00
|
|
|
sigHandler *signalHandler
|
2019-07-31 20:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type signalHandler struct {
|
2019-11-12 18:50:41 +00:00
|
|
|
sigChannel chan os.Signal
|
|
|
|
signals []os.Signal
|
2019-07-31 20:24:57 +00:00
|
|
|
}
|
|
|
|
|
2021-05-10 17:24:45 +00:00
|
|
|
type jwtPayload struct {
|
2020-02-24 02:48:06 +00:00
|
|
|
Aud []string `json:"aud"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
Exp int `json:"exp"`
|
|
|
|
Iat int `json:"iat"`
|
|
|
|
Nbf int `json:"nbf"`
|
|
|
|
Iss string `json:"iss"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Subt string `json:"sub"`
|
|
|
|
}
|
|
|
|
|
2020-11-09 03:25:35 +00:00
|
|
|
type transferServiceResponse struct {
|
|
|
|
AppToken string `json:"app_token"`
|
|
|
|
OrgToken string `json:"org_token"`
|
|
|
|
}
|
|
|
|
|
2021-05-10 17:24:45 +00:00
|
|
|
func (p jwtPayload) isExpired() bool {
|
2020-02-24 02:48:06 +00:00
|
|
|
return int(time.Now().Unix()) > p.Exp
|
|
|
|
}
|
|
|
|
|
2019-11-12 18:50:41 +00:00
|
|
|
func (s *signalHandler) register(handler func()) {
|
2019-07-31 20:24:57 +00:00
|
|
|
s.sigChannel = make(chan os.Signal, 1)
|
|
|
|
signal.Notify(s.sigChannel, s.signals...)
|
|
|
|
go func(s *signalHandler) {
|
|
|
|
for range s.sigChannel {
|
|
|
|
handler()
|
|
|
|
}
|
|
|
|
}(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *signalHandler) deregister() {
|
|
|
|
signal.Stop(s.sigChannel)
|
|
|
|
close(s.sigChannel)
|
2019-06-26 15:48:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func errDeleteTokenFailed(lockFilePath string) error {
|
|
|
|
return fmt.Errorf("failed to acquire a new Access token. Please try to delete %s", lockFilePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// newLock will get a new file lock
|
|
|
|
func newLock(path string) *lock {
|
|
|
|
lockPath := path + ".lock"
|
|
|
|
return &lock{
|
|
|
|
lockFilePath: lockPath,
|
2021-03-26 04:04:56 +00:00
|
|
|
backoff: &retry.BackoffHandler{MaxRetries: 7},
|
2019-11-12 18:50:41 +00:00
|
|
|
sigHandler: &signalHandler{
|
|
|
|
signals: []os.Signal{syscall.SIGINT, syscall.SIGTERM},
|
2019-07-31 20:24:57 +00:00
|
|
|
},
|
2019-06-26 15:48:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *lock) Acquire() error {
|
2019-07-31 20:24:57 +00:00
|
|
|
// Intercept SIGINT and SIGTERM to release lock before exiting
|
|
|
|
l.sigHandler.register(func() {
|
2020-11-25 06:55:13 +00:00
|
|
|
_ = l.deleteLockFile()
|
2019-11-12 18:50:41 +00:00
|
|
|
os.Exit(0)
|
2019-07-31 20:24:57 +00:00
|
|
|
})
|
|
|
|
|
2021-03-08 16:46:23 +00:00
|
|
|
// Check for a lock file
|
2019-06-26 15:48:45 +00:00
|
|
|
// if the lock file exists; start polling
|
|
|
|
// if not, create the lock file and go through the normal flow.
|
|
|
|
// See AUTH-1736 for the reason why we do all this
|
|
|
|
for isTokenLocked(l.lockFilePath) {
|
|
|
|
if l.backoff.Backoff(context.Background()) {
|
|
|
|
continue
|
2019-07-31 20:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := l.deleteLockFile(); err != nil {
|
|
|
|
return err
|
2019-06-26 15:48:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a lock file so other processes won't also try to get the token at
|
|
|
|
// the same time
|
|
|
|
if err := ioutil.WriteFile(l.lockFilePath, []byte{}, 0600); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-31 20:24:57 +00:00
|
|
|
func (l *lock) deleteLockFile() error {
|
2019-06-26 15:48:45 +00:00
|
|
|
if err := os.Remove(l.lockFilePath); err != nil && !os.IsNotExist(err) {
|
|
|
|
return errDeleteTokenFailed(l.lockFilePath)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-31 20:24:57 +00:00
|
|
|
func (l *lock) Release() error {
|
|
|
|
defer l.sigHandler.deregister()
|
|
|
|
return l.deleteLockFile()
|
|
|
|
}
|
|
|
|
|
2019-06-26 15:48:45 +00:00
|
|
|
// isTokenLocked checks to see if there is another process attempting to get the token already
|
|
|
|
func isTokenLocked(lockFilePath string) bool {
|
|
|
|
exists, err := config.FileExists(lockFilePath)
|
|
|
|
return exists && err == nil
|
|
|
|
}
|
|
|
|
|
2022-06-24 18:51:53 +00:00
|
|
|
func Init(version string) {
|
|
|
|
userAgent = fmt.Sprintf("cloudflared/%s", version)
|
|
|
|
}
|
|
|
|
|
2020-06-11 17:02:34 +00:00
|
|
|
// FetchTokenWithRedirect will either load a stored token or generate a new one
|
2020-07-17 16:08:05 +00:00
|
|
|
// it appends the full url as the redirect URL to the access cli request if opening the browser
|
2021-03-10 21:52:35 +00:00
|
|
|
func FetchTokenWithRedirect(appURL *url.URL, appInfo *AppInfo, log *zerolog.Logger) (string, error) {
|
|
|
|
return getToken(appURL, appInfo, false, log)
|
2020-06-11 17:02:34 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 15:18:23 +00:00
|
|
|
// FetchToken will either load a stored token or generate a new one
|
2020-07-17 16:08:05 +00:00
|
|
|
// it appends the host of the appURL as the redirect URL to the access cli request if opening the browser
|
2021-03-10 21:52:35 +00:00
|
|
|
func FetchToken(appURL *url.URL, appInfo *AppInfo, log *zerolog.Logger) (string, error) {
|
|
|
|
return getToken(appURL, appInfo, true, log)
|
2020-06-11 17:02:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getToken will either load a stored token or generate a new one
|
2021-03-10 21:52:35 +00:00
|
|
|
func getToken(appURL *url.URL, appInfo *AppInfo, useHostOnly bool, log *zerolog.Logger) (string, error) {
|
|
|
|
if token, err := GetAppTokenIfExists(appInfo); token != "" && err == nil {
|
2018-10-08 19:20:28 +00:00
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
|
2021-03-10 21:52:35 +00:00
|
|
|
appTokenPath, err := GenerateAppTokenFilePathFromURL(appInfo.AppDomain, appInfo.AppAUD, keyName)
|
2018-10-08 19:20:28 +00:00
|
|
|
if err != nil {
|
2020-11-09 03:25:35 +00:00
|
|
|
return "", errors.Wrap(err, "failed to generate app token file path")
|
2018-10-08 19:20:28 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 03:25:35 +00:00
|
|
|
fileLockAppToken := newLock(appTokenPath)
|
|
|
|
if err = fileLockAppToken.Acquire(); err != nil {
|
|
|
|
return "", errors.Wrap(err, "failed to acquire app token lock")
|
2019-06-26 15:48:45 +00:00
|
|
|
}
|
2020-11-09 03:25:35 +00:00
|
|
|
defer fileLockAppToken.Release()
|
2019-06-26 15:48:45 +00:00
|
|
|
|
|
|
|
// check to see if another process has gotten a token while we waited for the lock
|
2021-03-10 21:52:35 +00:00
|
|
|
if token, err := GetAppTokenIfExists(appInfo); token != "" && err == nil {
|
2019-06-26 15:48:45 +00:00
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
|
2021-11-12 15:38:06 +00:00
|
|
|
// If an app token couldn't be found on disk, check for an org token and attempt to exchange it for an app token.
|
2020-11-09 03:25:35 +00:00
|
|
|
var orgTokenPath string
|
2021-03-10 21:52:35 +00:00
|
|
|
orgToken, err := GetOrgTokenIfExists(appInfo.AuthDomain)
|
|
|
|
if err != nil {
|
|
|
|
orgTokenPath, err = generateOrgTokenFilePathFromURL(appInfo.AuthDomain)
|
2020-11-09 03:25:35 +00:00
|
|
|
if err != nil {
|
2021-03-10 21:52:35 +00:00
|
|
|
return "", errors.Wrap(err, "failed to generate org token file path")
|
|
|
|
}
|
2020-11-09 03:25:35 +00:00
|
|
|
|
2021-03-10 21:52:35 +00:00
|
|
|
fileLockOrgToken := newLock(orgTokenPath)
|
|
|
|
if err = fileLockOrgToken.Acquire(); err != nil {
|
|
|
|
return "", errors.Wrap(err, "failed to acquire org token lock")
|
2020-11-09 03:25:35 +00:00
|
|
|
}
|
2021-03-10 21:52:35 +00:00
|
|
|
defer fileLockOrgToken.Release()
|
|
|
|
// check if an org token has been created since the lock was acquired
|
|
|
|
orgToken, err = GetOrgTokenIfExists(appInfo.AuthDomain)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
if appToken, err := exchangeOrgToken(appURL, orgToken); err != nil {
|
|
|
|
log.Debug().Msgf("failed to exchange org token for app token: %s", err)
|
|
|
|
} else {
|
|
|
|
// generate app path
|
|
|
|
if err := ioutil.WriteFile(appTokenPath, []byte(appToken), 0600); err != nil {
|
|
|
|
return "", errors.Wrap(err, "failed to write app token to disk")
|
2020-11-09 03:25:35 +00:00
|
|
|
}
|
2021-03-10 21:52:35 +00:00
|
|
|
return appToken, nil
|
2020-11-09 03:25:35 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-25 06:55:13 +00:00
|
|
|
return getTokensFromEdge(appURL, appTokenPath, orgTokenPath, useHostOnly, log)
|
2020-11-09 03:25:35 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// getTokensFromEdge will attempt to use the transfer service to retrieve an app and org token, save them to disk,
|
|
|
|
// and return the app token.
|
2020-11-25 06:55:13 +00:00
|
|
|
func getTokensFromEdge(appURL *url.URL, appTokenPath, orgTokenPath string, useHostOnly bool, log *zerolog.Logger) (string, error) {
|
2021-11-12 15:38:06 +00:00
|
|
|
// If no org token exists or if it couldn't be exchanged for an app token, then run the transfer service flow.
|
2020-11-09 03:25:35 +00:00
|
|
|
|
2018-10-08 19:20:28 +00:00
|
|
|
// this weird parameter is the resource name (token) and the key/value
|
|
|
|
// we want to send to the transfer service. the key is token and the value
|
|
|
|
// is blank (basically just the id generated in the transfer service)
|
2021-03-08 16:46:23 +00:00
|
|
|
resourceData, err := RunTransfer(appURL, keyName, keyName, "", true, useHostOnly, log)
|
2018-10-08 19:20:28 +00:00
|
|
|
if err != nil {
|
2020-11-09 03:25:35 +00:00
|
|
|
return "", errors.Wrap(err, "failed to run transfer service")
|
|
|
|
}
|
|
|
|
var resp transferServiceResponse
|
|
|
|
if err = json.Unmarshal(resourceData, &resp); err != nil {
|
|
|
|
return "", errors.Wrap(err, "failed to marshal transfer service response")
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we were able to get the auth domain and generate an org token path, lets write it to disk.
|
|
|
|
if orgTokenPath != "" {
|
|
|
|
if err := ioutil.WriteFile(orgTokenPath, []byte(resp.OrgToken), 0600); err != nil {
|
|
|
|
return "", errors.Wrap(err, "failed to write org token to disk")
|
|
|
|
}
|
2018-10-08 19:20:28 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 03:25:35 +00:00
|
|
|
if err := ioutil.WriteFile(appTokenPath, []byte(resp.AppToken), 0600); err != nil {
|
|
|
|
return "", errors.Wrap(err, "failed to write app token to disk")
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.AppToken, nil
|
|
|
|
|
2018-10-08 19:20:28 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 21:52:35 +00:00
|
|
|
// GetAppInfo makes a request to the appURL and stops at the first redirect. The 302 location header will contain the
|
2020-11-09 03:25:35 +00:00
|
|
|
// auth domain
|
2021-03-10 21:52:35 +00:00
|
|
|
func GetAppInfo(reqURL *url.URL) (*AppInfo, error) {
|
2020-11-09 03:25:35 +00:00
|
|
|
client := &http.Client{
|
|
|
|
// do not follow redirects
|
|
|
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
2021-03-10 21:52:35 +00:00
|
|
|
// stop after hitting login endpoint since it will contain app path
|
|
|
|
if strings.Contains(via[len(via)-1].URL.Path, AccessLoginWorkerPath) {
|
|
|
|
return http.ErrUseLastResponse
|
|
|
|
}
|
|
|
|
return nil
|
2020-11-09 03:25:35 +00:00
|
|
|
},
|
|
|
|
Timeout: time.Second * 7,
|
|
|
|
}
|
|
|
|
|
2021-03-10 21:52:35 +00:00
|
|
|
appInfoReq, err := http.NewRequest("HEAD", reqURL.String(), nil)
|
2020-11-09 03:25:35 +00:00
|
|
|
if err != nil {
|
2021-03-10 21:52:35 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to create app info request")
|
2020-11-09 03:25:35 +00:00
|
|
|
}
|
2022-06-24 18:51:53 +00:00
|
|
|
appInfoReq.Header.Add("User-Agent", userAgent)
|
2021-03-10 21:52:35 +00:00
|
|
|
resp, err := client.Do(appInfoReq)
|
2020-11-09 03:25:35 +00:00
|
|
|
if err != nil {
|
2021-03-10 21:52:35 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to get app info")
|
2020-11-09 03:25:35 +00:00
|
|
|
}
|
|
|
|
resp.Body.Close()
|
2021-04-20 17:04:00 +00:00
|
|
|
|
|
|
|
var aud string
|
2021-03-10 21:52:35 +00:00
|
|
|
location := resp.Request.URL
|
2021-04-20 17:04:00 +00:00
|
|
|
if strings.Contains(location.Path, AccessLoginWorkerPath) {
|
|
|
|
aud = resp.Request.URL.Query().Get("kid")
|
|
|
|
if aud == "" {
|
|
|
|
return nil, errors.New("Empty app aud")
|
|
|
|
}
|
|
|
|
} else if audHeader := resp.Header.Get(appAUDHeader); audHeader != "" {
|
|
|
|
// 403/401 from the edge will have aud in a header
|
|
|
|
aud = audHeader
|
|
|
|
} else {
|
2021-04-05 18:05:11 +00:00
|
|
|
return nil, fmt.Errorf("failed to find Access application at %s", reqURL.String())
|
2021-03-10 21:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
domain := resp.Header.Get(appDomainHeader)
|
|
|
|
if domain == "" {
|
|
|
|
return nil, errors.New("Empty app domain")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &AppInfo{location.Hostname(), aud, domain}, nil
|
2020-11-09 03:25:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// exchangeOrgToken attaches an org token to a request to the appURL and returns an app token. This uses the Access SSO
|
|
|
|
// flow to automatically generate and return an app token without the login page.
|
|
|
|
func exchangeOrgToken(appURL *url.URL, orgToken string) (string, error) {
|
|
|
|
client := &http.Client{
|
|
|
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
|
|
|
// attach org token to login request
|
2021-03-10 21:52:35 +00:00
|
|
|
if strings.Contains(req.URL.Path, AccessLoginWorkerPath) {
|
|
|
|
req.AddCookie(&http.Cookie{Name: tokenCookie, Value: orgToken})
|
2020-11-09 03:25:35 +00:00
|
|
|
}
|
|
|
|
// stop after hitting authorized endpoint since it will contain the app token
|
|
|
|
if strings.Contains(via[len(via)-1].URL.Path, "cdn-cgi/access/authorized") {
|
|
|
|
return http.ErrUseLastResponse
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Timeout: time.Second * 7,
|
|
|
|
}
|
|
|
|
|
|
|
|
appTokenRequest, err := http.NewRequest("HEAD", appURL.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "failed to create app token request")
|
|
|
|
}
|
2022-06-24 18:51:53 +00:00
|
|
|
appTokenRequest.Header.Add("User-Agent", userAgent)
|
2020-11-09 03:25:35 +00:00
|
|
|
resp, err := client.Do(appTokenRequest)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "failed to get app token")
|
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
var appToken string
|
|
|
|
for _, c := range resp.Cookies() {
|
2021-02-09 17:49:16 +00:00
|
|
|
//if Org token revoked on exchange, getTokensFromEdge instead
|
2021-03-10 21:52:35 +00:00
|
|
|
validAppToken := c.Name == tokenCookie && time.Now().Before(c.Expires)
|
2021-02-09 17:49:16 +00:00
|
|
|
if validAppToken {
|
2020-11-09 03:25:35 +00:00
|
|
|
appToken = c.Value
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(appToken) > 0 {
|
|
|
|
return appToken, nil
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("response from %s did not contain app token", resp.Request.URL.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetOrgTokenIfExists(authDomain string) (string, error) {
|
2021-03-08 16:46:23 +00:00
|
|
|
path, err := generateOrgTokenFilePathFromURL(authDomain)
|
2018-10-08 19:20:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2020-11-09 03:25:35 +00:00
|
|
|
token, err := getTokenIfExists(path)
|
2018-10-08 19:20:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-05-10 17:24:45 +00:00
|
|
|
var payload jwtPayload
|
2022-03-18 17:11:44 +00:00
|
|
|
err = json.Unmarshal(token.UnsafePayloadWithoutVerification(), &payload)
|
2018-10-08 19:20:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-11-09 03:25:35 +00:00
|
|
|
if payload.isExpired() {
|
|
|
|
err := os.Remove(path)
|
|
|
|
return "", err
|
|
|
|
}
|
2022-03-18 17:11:44 +00:00
|
|
|
return token.CompactSerialize()
|
2020-11-09 03:25:35 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 21:52:35 +00:00
|
|
|
func GetAppTokenIfExists(appInfo *AppInfo) (string, error) {
|
|
|
|
path, err := GenerateAppTokenFilePathFromURL(appInfo.AppDomain, appInfo.AppAUD, keyName)
|
2020-11-09 03:25:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
token, err := getTokenIfExists(path)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-05-10 17:24:45 +00:00
|
|
|
var payload jwtPayload
|
2022-03-18 17:11:44 +00:00
|
|
|
err = json.Unmarshal(token.UnsafePayloadWithoutVerification(), &payload)
|
2020-02-24 02:48:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if payload.isExpired() {
|
|
|
|
err := os.Remove(path)
|
|
|
|
return "", err
|
|
|
|
}
|
2022-03-18 17:11:44 +00:00
|
|
|
return token.CompactSerialize()
|
2020-11-09 03:25:35 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTokenIfExists will return the token from local storage if it exists and not expired
|
2022-03-18 17:11:44 +00:00
|
|
|
func getTokenIfExists(path string) (*jose.JSONWebSignature, error) {
|
2020-11-09 03:25:35 +00:00
|
|
|
content, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-18 17:11:44 +00:00
|
|
|
token, err := jose.ParseSigned(string(content))
|
2020-11-09 03:25:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-18 17:11:44 +00:00
|
|
|
return token, nil
|
2019-06-26 15:48:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveTokenIfExists removes the a token from local storage if it exists
|
2021-03-10 21:52:35 +00:00
|
|
|
func RemoveTokenIfExists(appInfo *AppInfo) error {
|
|
|
|
path, err := GenerateAppTokenFilePathFromURL(appInfo.AppDomain, appInfo.AppAUD, keyName)
|
2018-10-08 19:20:28 +00:00
|
|
|
if err != nil {
|
2019-06-26 15:48:45 +00:00
|
|
|
return err
|
2018-10-08 19:20:28 +00:00
|
|
|
}
|
2019-06-26 15:48:45 +00:00
|
|
|
if err := os.Remove(path); err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
2018-10-08 19:20:28 +00:00
|
|
|
}
|
2019-06-26 15:48:45 +00:00
|
|
|
|
|
|
|
return nil
|
2018-10-08 19:20:28 +00:00
|
|
|
}
|