Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored and fixed device code sample #1

Merged
merged 13 commits into from
Jun 15, 2020
2 changes: 1 addition & 1 deletion getpackages.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Rem getpackages
Rem push ./src

go get -u github.com/sirupsen/logrus
go get -u github.com/shirou/gopsutil/host
go get -u github.com/shirou/gopsutil@v1
go get -u github.com/twinj/uuid
go get -u golang.org/x/crypto/ssh/terminal
go get -u github.com/AzureAD\microsoft-authentication-library-for-go/src/internal/msalbase
Expand Down
13 changes: 13 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module github.com/AzureAD/microsoft-authentication-library-for-go

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/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
)
26 changes: 26 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d h1:G0m3OIz70MZUWq3EgK3CesDbo8upS2Vm9/P3FtgI+Jk=
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-ole/go-ole v1.2.4 h1:nNBDSCOigTSiarFpYE9J/KtEA1IOW4CNeqT9TQDqCxI=
github.com/go-ole/go-ole v1.2.4/go.mod h1:XCwSNxSkXRo4vlyPy93sltvi/qJq0jqQhjqQNIwKuxM=
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/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=
2 changes: 1 addition & 1 deletion src/AcquireTokenCommonParameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

package msalgo

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

type acquireTokenCommonParameters struct {
scopes []string
Expand Down
2 changes: 1 addition & 1 deletion src/AcquireTokenDeviceCodeParameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

package msalgo

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

// AcquireTokenDeviceCodeParameters stuff
type AcquireTokenDeviceCodeParameters struct {
Expand Down
2 changes: 1 addition & 1 deletion src/AcquireTokenInteractiveParameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

package msalgo

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

// AcquireTokenInteractiveParameters stuff
type AcquireTokenInteractiveParameters struct {
Expand Down
2 changes: 1 addition & 1 deletion src/AcquireTokenSilentParameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

package msalgo

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

// AcquireTokenSilentParameters stuff
type AcquireTokenSilentParameters struct {
Expand Down
2 changes: 1 addition & 1 deletion src/AcquireTokenUsernamePasswordParameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

package msalgo

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

// AcquireTokenUsernamePasswordParameters stuff
type AcquireTokenUsernamePasswordParameters struct {
Expand Down
2 changes: 1 addition & 1 deletion src/ApplicationCommonParameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

package msalgo

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

type applicationCommonParameters struct {
clientID string
Expand Down
6 changes: 3 additions & 3 deletions src/PublicClientApplication.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
package msalgo

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

// PublicClientApplication is used to acquire tokens in desktop or mobile applications (Desktop / UWP / Xamarin.iOS / Xamarin.Android).
Expand Down
2 changes: 1 addition & 1 deletion src/PublicClientApplicationParameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

package msalgo

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

// PublicClientApplicationParameters stuff
type PublicClientApplicationParameters struct {
Expand Down
47 changes: 47 additions & 0 deletions src/examples/devicecodeflow/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

package main

import (
log "github.com/sirupsen/logrus"

msalgo "github.com/AzureAD/microsoft-authentication-library-for-go/src"
)

// CLIENTID is a UUID issued by the authorization server for your application
const CLIENTID string = "0615b6ca-88d4-4884-8729-b178178f7c27"

// AUTHORITY is a URL that defines token authority
const AUTHORITY string = "https://login.microsoftonline.com/organizations"

//SCOPES are requested to access a protected API
var SCOPES []string = []string{"user.read"}

//createPCAParams is used to instantiate the parameters to create the Public Client Application
func createPCAParams() *msalgo.PublicClientApplicationParameters {
pcaParams := msalgo.CreatePublicClientApplicationParameters(CLIENTID)
pcaParams.SetAadAuthority(AUTHORITY)
return pcaParams
}

func acquireTokenDeviceCode() {
// Creating the Public Client Application
pcaParams := createPCAParams()
publicClientApp, err := msalgo.CreatePublicClientApplication(pcaParams)
if err != nil {
log.Fatal(err)
}

deviceCodeParams := msalgo.CreateAcquireTokenDeviceCodeParameters(SCOPES)
result, err := publicClientApp.AcquireTokenByDeviceCode(deviceCodeParams)
if err != nil {
log.Fatal(err)
}
accessToken := result.GetAccessToken()
log.Info("Access token is: " + accessToken)
}

func main() {
acquireTokenDeviceCode()
}
9 changes: 5 additions & 4 deletions src/examples/msalconsole/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

log "github.com/sirupsen/logrus"

"github.com/AzureAD/microsoft-authentication-library-for-go/src"
msalgo "github.com/AzureAD/microsoft-authentication-library-for-go/src"
"github.com/shirou/gopsutil/host"
"golang.org/x/crypto/ssh/terminal"
)
Expand Down Expand Up @@ -83,9 +83,10 @@ func main() {
h, _ := host.Info()
log.Infof("%#v", h)

// set this to get function names in the logs: log.SetReportCaller(true)
// set this to get function names in the logs:
log.SetReportCaller(true)
log.Info("creating pca")

acquireByDeviceCode()
//acquireByUsernamePassword()
//acquireByDeviceCode()
hchittanuru3 marked this conversation as resolved.
Show resolved Hide resolved
acquireByUsernamePassword()
}
4 changes: 4 additions & 0 deletions src/internal/msalbase/TokenResponse.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ func (tr *TokenResponse) IsAuthorizationPending() bool {
return tr.baseResponse.Error == "authorization_pending"
}

func (tr *TokenResponse) IsSlowDown() bool {
return tr.baseResponse.Error == "slow_down"
}

func (tr *TokenResponse) GetAccessToken() string {
return tr.accessToken
}
Expand Down
2 changes: 1 addition & 1 deletion src/internal/requests/AadInstanceDiscovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package requests
import (
"sync"

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

var instanceDiscoveryCache = map[string]*instanceDiscoveryMetadata{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

log "github.com/sirupsen/logrus"

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

type authorityEndpointCacheEntry struct {
Expand Down
21 changes: 14 additions & 7 deletions src/internal/requests/DeviceCodeRequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

log "github.com/sirupsen/logrus"

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

// DeviceCodeRequest stuff
Expand All @@ -32,6 +32,13 @@ func CreateDeviceCodeRequest(
func (req *DeviceCodeRequest) Execute() (*msalbase.TokenResponse, error) {

// resolve authority endpoints
resolutionManager := CreateAuthorityEndpointResolutionManager(req.webRequestManager)
endpoints, err := resolutionManager.ResolveEndpoints(req.authParameters.GetAuthorityInfo(), "")
if err != nil {
return nil, err
}

req.authParameters.SetAuthorityEndpoints(endpoints)

deviceCodeResult, err := req.webRequestManager.GetDeviceCodeResult(req.authParameters)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we update GetDeviceCodeResult to return IDeviceCodeResult? In line 50, req.deviceCodeCallback() can then be updated to be the callback set by the developer, instead of the internal callback?

if err != nil {
Expand All @@ -46,6 +53,8 @@ func (req *DeviceCodeRequest) Execute() (*msalbase.TokenResponse, error) {

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

interval := 5
hchittanuru3 marked this conversation as resolved.
Show resolved Hide resolved

timeRemaining := deviceCodeResult.GetExpiresOn().Sub(time.Now().UTC())

for timeRemaining.Seconds() > 0.0 {
Expand All @@ -57,18 +66,16 @@ func (req *DeviceCodeRequest) waitForTokenResponse(deviceCodeResult *msalbase.De
if err != nil {
if isErrorAuthorizationPending(err) {
timeRemaining = deviceCodeResult.GetExpiresOn().Sub(time.Now().UTC())
} else if isErrorSlowDown(err) {
interval += 5
} else {
return nil, err
}
} else {
if tokenResponse.IsAuthorizationPending() {
timeRemaining = deviceCodeResult.GetExpiresOn().Sub(time.Now().UTC())
} else {
return tokenResponse, nil
}
return tokenResponse, nil
}

time.Sleep(5 * time.Second)
time.Sleep(time.Duration(interval) * time.Second)
}

return nil, errors.New("Verification code expired before contacting the server")
Expand Down
22 changes: 3 additions & 19 deletions src/internal/requests/DeviceCodeResponse.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,19 @@ package requests

import (
"encoding/json"
"strconv"
"time"

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

type deviceCodeResponse struct {
BaseResponse *msalbase.OAuthResponseBase
UserCode string `json:"user_code"`
DeviceCode string `json:"device_code"`
VerificationURL string `json:"verification_url"`
ExpiresInStr string `json:"expires_in"`
IntervalStr string `json:"interval"`
ExpiresIn int `json:"expires_in"`
Interval int `json:"interval"`
Message string `json:"message"`

ExpiresIn int
Interval int
}

// createDeviceCodeResponse stuff
Expand All @@ -39,18 +35,6 @@ func createDeviceCodeResponse(responseCode int, responseData string) (*deviceCod

dcResponse.BaseResponse = baseResponse

expiresIn, err := strconv.Atoi(dcResponse.ExpiresInStr)
if err != nil {
return nil, err
}
dcResponse.ExpiresIn = expiresIn

interval, err := strconv.Atoi(dcResponse.IntervalStr)
if err != nil {
return nil, err
}
dcResponse.Interval = interval

return dcResponse, nil
}

Expand Down
2 changes: 1 addition & 1 deletion src/internal/requests/IRealmMetadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package requests

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

type IRealmMetadata interface {
Expand Down
2 changes: 1 addition & 1 deletion src/internal/requests/ITokenRequester.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

package requests

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

type ITokenRequester interface {
Execute() (*msalbase.TokenResponse, error)
Expand Down
5 changes: 3 additions & 2 deletions src/internal/requests/IWebRequestManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
package requests

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

hchittanuru3 marked this conversation as resolved.
Show resolved Hide resolved
"github.com/AzureAD/microsoft-authentication-library-for-go/src/internal/wstrust"
)

// IWebRequestManager interface
Expand Down
2 changes: 1 addition & 1 deletion src/internal/requests/InteractiveRequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package requests

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

// InteractiveRequest stuff
Expand Down
2 changes: 1 addition & 1 deletion src/internal/requests/NetworkedCacheManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package requests

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

type NetworkedCacheManager struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package requests
import (
"errors"

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

type IOpenIdConfigurationEndpointManager interface {
Expand Down
2 changes: 1 addition & 1 deletion src/internal/requests/RealmMetadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package requests

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

type RealmMetadata struct {
Expand Down
Loading