Skip to content

Commit

Permalink
Refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
Hemanth Chittanuru committed Jun 10, 2020
1 parent 89a47b9 commit 12ebd17
Show file tree
Hide file tree
Showing 21 changed files with 45 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"fmt"
"time"

msalgo "github.com/AzureAD/microsoft-authentication-library-for-go/src"
msalgo "github.com/AzureAD/microsoft-authentication-library-for-go/src/msal"
log "github.com/sirupsen/logrus"
)

Expand All @@ -31,7 +31,7 @@ func acquireTokenDeviceCode() {
}
cancelCtx, cancelFunc := context.WithTimeout(context.Background(), time.Duration(cancelTimeout)*time.Second)
defer cancelFunc()
deviceCodeParams := msalgo.CreateAcquireTokenDeviceCodeParameters(config.GetScopes(), deviceCodeCallback, cancelCtx)
deviceCodeParams := msalgo.CreateAcquireTokenDeviceCodeParameters(cancelCtx, config.GetScopes(), deviceCodeCallback)
resultChannel := make(chan msalgo.IAuthenticationResult)
errChannel := make(chan error)
go func() {
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/examples/SampleUtils.go → examples/SampleUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"io/ioutil"
"os"

msalgo "github.com/AzureAD/microsoft-authentication-library-for-go/src"
msalgo "github.com/AzureAD/microsoft-authentication-library-for-go/src/msal"
log "github.com/sirupsen/logrus"
)

Expand Down
File renamed without changes.
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ go 1.14
require (
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
github.com/go-ole/go-ole v1.2.4 // indirect
github.com/shirou/gopsutil v2.20.4+incompatible
github.com/shirou/gopsutil v2.20.5+incompatible
github.com/sirupsen/logrus v1.6.0
github.com/twinj/uuid v1.0.0
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37 // indirect
golang.org/x/sys v0.0.0-20200523222454-059865788121 // indirect
golang.org/x/sys v0.0.0-20200610111108-226ff32320da // indirect
)
15 changes: 4 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,14 @@ github.com/go-ole/go-ole v1.2.4/go.mod h1:XCwSNxSkXRo4vlyPy93sltvi/qJq0jqQhjqQNI
github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8=
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/shirou/gopsutil v2.20.4+incompatible h1:cMT4rxS55zx9NVUnCkrmXCsEB/RNfG9SwHY9evtX8Ng=
github.com/shirou/gopsutil v2.20.4+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/shirou/gopsutil v2.20.5+incompatible h1:tYH07UPoQt0OCQdgWWMgYHy3/a9bcxNpBIysykNIP7I=
github.com/shirou/gopsutil v2.20.5+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I=
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/twinj/uuid v1.0.0 h1:fzz7COZnDrXGTAOHGuUGYd6sG+JMq+AoE7+Jlu0przk=
github.com/twinj/uuid v1.0.0/go.mod h1:mMgcE1RHFUFqe5AfiwlINXisXfDGro23fWdPUfOMjRY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37 h1:cg5LA/zNPRzIXIWSCxQW10Rvpy94aQh3LT/ShoCpkHw=
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200523222454-059865788121 h1:rITEj+UZHYC927n8GT97eC3zrpzXdb/voyeOuVKS46o=
golang.org/x/sys v0.0.0-20200523222454-059865788121/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/sys v0.0.0-20200610111108-226ff32320da h1:bGb80FudwxpeucJUjPYJXuJ8Hk91vNtfvrymzwiei38=
golang.org/x/sys v0.0.0-20200610111108-226ff32320da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
7 changes: 3 additions & 4 deletions src/internal/requests/WebRequestManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ type WebRequestManager struct {
httpManager *msalbase.HTTPManager
}

func isErrorAuthorizationPending(err error) bool {
func IsErrorAuthorizationPending(err error) bool {
return err.Error() == "authorization_pending"
}

func isErrorSlowDown(err error) bool {
func IsErrorSlowDown(err error) bool {
return err.Error() == "slow_down"
}

Expand Down Expand Up @@ -222,8 +222,7 @@ func joinScopes(scopes []string) string {
func addScopeQueryParam(queryParams map[string]string, authParameters *msalbase.AuthParametersInternal) {
log.Trace("Adding scopes 'openid', 'offline_access', 'profile'")
requestedScopes := authParameters.GetScopes()

// openid equired to get an id token
// openid required to get an id token
// offline_access required to get a refresh token
// profile required to get the client_info field back
requestedScopes = append(requestedScopes, "openid", "offline_access", "profile")
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ type AcquireTokenDeviceCodeParameters struct {
}

// CreateAcquireTokenDeviceCodeParameters stuff
func CreateAcquireTokenDeviceCodeParameters(scopes []string,
deviceCodeCallback func(IDeviceCodeResult),
cancelCtx context.Context) *AcquireTokenDeviceCodeParameters {
func CreateAcquireTokenDeviceCodeParameters(cancelCtx context.Context, scopes []string,
deviceCodeCallback func(IDeviceCodeResult)) *AcquireTokenDeviceCodeParameters {
p := &AcquireTokenDeviceCodeParameters{
commonParameters: createAcquireTokenCommonParameters(scopes),
deviceCodeCallback: deviceCodeCallback,
Expand All @@ -33,12 +32,6 @@ func (p *AcquireTokenDeviceCodeParameters) augmentAuthenticationParameters(authP
authParams.SetAuthorizationType(msalbase.AuthorizationTypeDeviceCode)
}

func (p *AcquireTokenDeviceCodeParameters) InternalCallback(dcr *msalbase.DeviceCodeResult) {
var returnedDCR IDeviceCodeResult
returnedDCR = dcr
p.deviceCodeCallback(returnedDCR)
}

func (p *AcquireTokenDeviceCodeParameters) GetCancelContext() context.Context {
return p.cancelCtx
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type AcquireTokenSilentParameters struct {
}

// CreateAcquireTokenSilentParameters stuff
func CreateAcquireTokenSilentParameters(scopes []string, username string, password string) *AcquireTokenSilentParameters {
func CreateAcquireTokenSilentParameters(scopes []string) *AcquireTokenSilentParameters {
p := &AcquireTokenSilentParameters{
commonParameters: createAcquireTokenCommonParameters(scopes),
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

package requests
package msalgo

import (
"context"
Expand All @@ -11,32 +11,32 @@ import (
log "github.com/sirupsen/logrus"

"github.com/AzureAD/microsoft-authentication-library-for-go/src/internal/msalbase"
"github.com/AzureAD/microsoft-authentication-library-for-go/src/internal/requests"
)

// DeviceCodeRequest stuff
type DeviceCodeRequest struct {
webRequestManager IWebRequestManager
type deviceCodeRequest struct {
webRequestManager requests.IWebRequestManager
cacheManager msalbase.ICacheManager
authParameters *msalbase.AuthParametersInternal
deviceCodeCallback func(*msalbase.DeviceCodeResult)
//cancelChannel chan bool
cancelCtx context.Context
deviceCodeCallback func(IDeviceCodeResult)
cancelCtx context.Context
}

// CreateDeviceCodeRequest stuff
func CreateDeviceCodeRequest(
webRequestManager IWebRequestManager,
func createDeviceCodeRequest(cancelCtx context.Context,
webRequestManager requests.IWebRequestManager,
cacheManager msalbase.ICacheManager,
authParameters *msalbase.AuthParametersInternal,
deviceCodeCallback func(*msalbase.DeviceCodeResult), cancelCtx context.Context) *DeviceCodeRequest {
req := &DeviceCodeRequest{webRequestManager, cacheManager, authParameters, deviceCodeCallback, cancelCtx}
deviceCodeCallback func(IDeviceCodeResult)) *deviceCodeRequest {
req := &deviceCodeRequest{webRequestManager, cacheManager, authParameters, deviceCodeCallback, cancelCtx}
return req
}

// Execute stuff
func (req *DeviceCodeRequest) Execute() (*msalbase.TokenResponse, error) {
func (req *deviceCodeRequest) Execute() (*msalbase.TokenResponse, error) {
// resolve authority endpoints
resolutionManager := CreateAuthorityEndpointResolutionManager(req.webRequestManager)
resolutionManager := requests.CreateAuthorityEndpointResolutionManager(req.webRequestManager)
endpoints, err := resolutionManager.ResolveEndpoints(req.authParameters.GetAuthorityInfo(), "")
if err != nil {
return nil, err
Expand All @@ -53,7 +53,7 @@ func (req *DeviceCodeRequest) Execute() (*msalbase.TokenResponse, error) {
return req.waitForTokenResponse(deviceCodeResult)
}

func (req *DeviceCodeRequest) waitForTokenResponse(deviceCodeResult *msalbase.DeviceCodeResult) (*msalbase.TokenResponse, error) {
func (req *deviceCodeRequest) waitForTokenResponse(deviceCodeResult *msalbase.DeviceCodeResult) (*msalbase.TokenResponse, error) {

interval := deviceCodeResult.GetInterval()
timeRemaining := deviceCodeResult.GetExpiresOn().Sub(time.Now().UTC())
Expand All @@ -65,9 +65,9 @@ func (req *DeviceCodeRequest) waitForTokenResponse(deviceCodeResult *msalbase.De
default:
tokenResponse, err := req.webRequestManager.GetAccessTokenFromDeviceCodeResult(req.authParameters, deviceCodeResult)
if err != nil {
if isErrorAuthorizationPending(err) {
if requests.IsErrorAuthorizationPending(err) {
timeRemaining = deviceCodeResult.GetExpiresOn().Sub(time.Now().UTC())
} else if isErrorSlowDown(err) {
} else if requests.IsErrorSlowDown(err) {
interval += 5
} else {
return nil, err
Expand Down
File renamed without changes.
File renamed without changes.
14 changes: 14 additions & 0 deletions src/msal/IClientApplication.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

package msalgo

import (
"github.com/AzureAD/microsoft-authentication-library-for-go/src/internal/msalbase"
"github.com/AzureAD/microsoft-authentication-library-for-go/src/internal/requests"
)

type IClientApplication interface {
AcquireTokenSilent(*AcquireTokenSilentParameters) (IAuthenticationResult, error)
executeTokenRequestWithoutCacheWrite(requests.ITokenRequester, *msalbase.AuthParametersInternal) (IAuthenticationResult, error)
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ func (pca *PublicClientApplication) AcquireTokenByDeviceCode(
deviceCodeParameters *AcquireTokenDeviceCodeParameters) (IAuthenticationResult, error) {
authParams := pca.pcaParameters.createAuthenticationParameters()
deviceCodeParameters.augmentAuthenticationParameters(authParams)
req := requests.CreateDeviceCodeRequest(pca.webRequestManager, pca.cacheManager, authParams,
deviceCodeParameters.InternalCallback, deviceCodeParameters.GetCancelContext())
req := createDeviceCodeRequest(deviceCodeParameters.GetCancelContext(), pca.webRequestManager, pca.cacheManager, authParams, deviceCodeParameters.deviceCodeCallback)
return pca.executeTokenRequestWithoutCacheWrite(req, authParams)
}

Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit 12ebd17

Please sign in to comment.