Skip to content

Commit

Permalink
Fixing AzureCLI / CloudShell authentication
Browse files Browse the repository at this point in the history
 - Hard-coding the value for `expiresIn` due to the Azure CLI and CloudShell authentication models having different types
 - Updating `expiresOn` to be the difference in seconds between the baseDate & now - rather than a date
 - Parsing the AzureCLI time out in the local system timezone, given it's saved in local time but with no timezone info
  • Loading branch information
tombuildsstuff committed Sep 9, 2017
1 parent b5c0d2a commit a04d94c
Showing 1 changed file with 38 additions and 6 deletions.
44 changes: 38 additions & 6 deletions autorest/adal/cli.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package adal

import (
"github.com/mitchellh/go-homedir"
"fmt"
"log"
"strconv"
"time"

"github.com/mitchellh/go-homedir"
)

// AzureCLIToken represents an AccessToken from the Azure CLI
type AzureCLIToken struct {
AccessToken string `json:"accessToken"`
Authority string `json:"_authority"`
ClientID string `json:"_clientId"`
ExpiresIn int `json:"expiresIn"`
ExpiresOn string `json:"expiresOn"`
IdentityProvider string `json:"identityProvider"`
IsMRRT bool `json:"isMRRT"`
Expand Down Expand Up @@ -47,13 +50,42 @@ func AzureCLIProfilePath() (string, error) {
}

// ToToken converts an AzureCLIToken to a Token
func (t AzureCLIToken) ToToken() Token {
return Token{
func (t AzureCLIToken) ToToken() (*Token, error) {
tokenExpirationDate, err := ParseAzureCLIExpirationDate(t.ExpiresOn)
if err != nil {
return nil, fmt.Errorf("Error parsing Token Expiration Date %q: %+v", t.ExpiresOn, err)
}

difference := tokenExpirationDate.Sub(expirationBase)
seconds := difference.Seconds()

token := Token{
AccessToken: t.AccessToken,
Type: t.TokenType,
ExpiresIn: strconv.Itoa(t.ExpiresIn),
ExpiresOn: t.ExpiresOn,
ExpiresIn: "3600",
ExpiresOn: strconv.Itoa(int(seconds)),
RefreshToken: t.RefreshToken,
Resource: t.Resource,
}
return &token, nil
}

// ParseAzureCLIExpirationDate parses either a Azure CLI or CloudShell date into a time object
func ParseAzureCLIExpirationDate(input string) (*time.Time, error) {
log.Printf("[DEBUG] Token Date: %s", input)

// CloudShell (and potentially the Azure CLI in future)
expirationDate, cloudShellErr := time.Parse(time.RFC3339, input)
if cloudShellErr != nil {
// Azure CLI (Python) e.g. 2017-08-31 19:48:57.998857 (plus the local timezone)
cliFormat := "2006-01-02 15:04:05.999999"
expirationDate, cliErr := time.ParseInLocation(cliFormat, input, time.Local)
if cliErr == nil {
return &expirationDate, nil
}

return nil, fmt.Errorf("Error parsing expiration date %q.\n\nCloudShell Error: \n%+v\n\nCLI Error:\n%+v", input, cloudShellErr, cliErr)
}

return &expirationDate, nil
}

0 comments on commit a04d94c

Please sign in to comment.