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
24 changes: 20 additions & 4 deletions src/AcquireTokenDeviceCodeParameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@

package msalgo

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

// AcquireTokenDeviceCodeParameters stuff
type AcquireTokenDeviceCodeParameters struct {
commonParameters *acquireTokenCommonParameters
commonParameters *acquireTokenCommonParameters
deviceCodeCallback func(IDeviceCodeResult)
cancelChannel chan bool
}

// CreateAcquireTokenDeviceCodeParameters stuff
func CreateAcquireTokenDeviceCodeParameters(scopes []string) *AcquireTokenDeviceCodeParameters {
func CreateAcquireTokenDeviceCodeParameters(scopes []string,
deviceCodeCallback func(IDeviceCodeResult),
cancelChannel chan bool) *AcquireTokenDeviceCodeParameters {
p := &AcquireTokenDeviceCodeParameters{
commonParameters: createAcquireTokenCommonParameters(scopes),
commonParameters: createAcquireTokenCommonParameters(scopes),
deviceCodeCallback: deviceCodeCallback,
cancelChannel: cancelChannel,
}
return p
}
Expand All @@ -22,3 +28,13 @@ func (p *AcquireTokenDeviceCodeParameters) augmentAuthenticationParameters(authP
p.commonParameters.augmentAuthenticationParameters(authParams)
authParams.SetAuthorizationType(msalbase.AuthorizationTypeDeviceCode)
}

func (p *AcquireTokenDeviceCodeParameters) InternalCallback(dcr *msalbase.DeviceCodeResult) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need an internal callback? Can we just call deviceCodeCallback directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is to cast it to the interface IDeviceCodeResult. The internal library returns a DeviceCodeResult, and to make it accessible to the user, we utilize IDeviceCodeResult.

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 use the interface in the method signatures internally as well? We should try to use the interface wherever possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The interface is not accessible to the internal package. The DeviceCodeRequest uses this callback and passes in a DeviceCodeResult, which this InternalCallback casts to the interface type IDeviceCodeResult.

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 make the interface accessible to the internal package?

var returnedDCR IDeviceCodeResult
returnedDCR = dcr
p.deviceCodeCallback(returnedDCR)
}

func (p *AcquireTokenDeviceCodeParameters) GetCancelChannel() chan bool {
return p.cancelChannel
}
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
17 changes: 17 additions & 0 deletions src/IDeviceCodeResult.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

package msalgo

import "time"

// IDeviceCodeResult is an interface for DeviceCodeResult
type IDeviceCodeResult interface {
GetMessage() string
String() string
GetUserCode() string
GetDeviceCode() string
GetVerificationURL() string
GetExpiresOn() time.Time
GetInterval() int
}
17 changes: 8 additions & 9 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 All @@ -18,7 +18,7 @@ type PublicClientApplication struct {
cacheManager msalbase.ICacheManager
}

// CreatePublicClientApplication stuff
// CreatePublicClientApplication creates a PublicClientApplication Instance given its parameters, which include client ID and authority info
hchittanuru3 marked this conversation as resolved.
Show resolved Hide resolved
func CreatePublicClientApplication(pcaParameters *PublicClientApplicationParameters) (*PublicClientApplication, error) {
err := pcaParameters.validate()
if err != nil {
Expand Down Expand Up @@ -70,19 +70,18 @@ func (pca *PublicClientApplication) AcquireTokenByDeviceCode(
deviceCodeParameters *AcquireTokenDeviceCodeParameters) (IAuthenticationResult, error) {
authParams := pca.pcaParameters.createAuthenticationParameters()
deviceCodeParameters.augmentAuthenticationParameters(authParams)

req := requests.CreateDeviceCodeRequest(pca.webRequestManager, pca.cacheManager, authParams)
req := requests.CreateDeviceCodeRequest(pca.webRequestManager, pca.cacheManager, authParams,
deviceCodeParameters.InternalCallback, deviceCodeParameters.GetCancelChannel())
return pca.executeTokenRequestWithoutCacheWrite(req, authParams)
}

// AcquireTokenInteractive stuff
func (pca *PublicClientApplication) AcquireTokenInteractive(
interactiveParams *AcquireTokenInteractiveParameters) (IAuthenticationResult, error) {
interactiveParams *AcquireTokenInteractiveParameters, code string) (IAuthenticationResult, error) {
authParams := pca.pcaParameters.createAuthenticationParameters()
interactiveParams.augmentAuthenticationParameters(authParams)

req := requests.CreateInteractiveRequest(pca.webRequestManager, pca.cacheManager, authParams)
return pca.executeTokenRequestWithCacheWrite(req, authParams)
return pca.executeTokenRequestWithoutCacheWrite(req, authParams)
}

// executeTokenRequestWithoutCacheWrite stuff
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/DeviceCodeFlowSample.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 (
"fmt"
"time"

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

func deviceCodeCallback(deviceCodeResult msalgo.IDeviceCodeResult) {
log.Infof(deviceCodeResult.GetMessage())
}

func setCancelTimeout(seconds int, cancelChannel chan bool) {
time.Sleep(time.Duration(seconds) * time.Second)
cancelChannel <- true
}

func acquireTokenDeviceCode() {
cancelTimeout := 100 //Change this for cancel timeout
config := CreateConfig("config.json")
pcaParams := createPCAParams(config.GetClientID(), config.GetAuthority())
sangonzal marked this conversation as resolved.
Show resolved Hide resolved
publicClientApp, err := msalgo.CreatePublicClientApplication(pcaParams)
if err != nil {
log.Fatal(err)
}
cancelChannel := make(chan bool)
deviceCodeParams := msalgo.CreateAcquireTokenDeviceCodeParameters(config.GetScopes(), deviceCodeCallback, cancelChannel)
resultChannel := make(chan msalgo.IAuthenticationResult)
errChannel := make(chan error)
go func() {
result, err := publicClientApp.AcquireTokenByDeviceCode(deviceCodeParams)
errChannel <- err
resultChannel <- result
}()
go setCancelTimeout(cancelTimeout, cancelChannel)
err = <-errChannel
if err != nil {
log.Fatal(err)
}
result := <-resultChannel
fmt.Println("Access token is " + result.GetAccessToken())
}
18 changes: 18 additions & 0 deletions src/examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Running Examples for MSAL Go

To run one of the examples of uses of MSAL Go, you need to first create a `config.json` file. The `config.json` file should look like the following:
```json
{
"authority": "https://login.microsoftonline.com/organizations",
"client_id": "your_client_id",
"scopes": ["user.read"],
// You can find the other permission names from this document
// https://docs.microsoft.com/en-us/graph/permissions-reference
"username": "your_username",
"password": "your_password" //This is a sample only. DO NOT persist your password.
}
```

To run one of the examples, run the command `go run src/examples/*.go <example-arg>`. The example arguments are as follows:
* 1 - `DeviceCodeFlowSample.go` without cancellation timeout
* 2 - `UsernamePasswordPublicFlowSample.go`
70 changes: 70 additions & 0 deletions src/examples/SampleUtils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

package main

import (
"encoding/json"
"io/ioutil"
"os"

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

//Config represents the config.json required to run the samples
type Config struct {
ClientID string `json:"client_id"`
Authority string `json:"authority"`
Scopes []string `json:"scopes"`
Username string `json:"username"`
Password string `json:"password"`
}

//CreateConfig creates the Config struct from a json file
func CreateConfig(fileName string) *Config {
jsonFile, err := os.Open(fileName)
if err != nil {
log.Fatal(err)
}
defer jsonFile.Close()
data, err := ioutil.ReadAll(jsonFile)
config := &Config{}
err = json.Unmarshal(data, config)
if err != nil {
log.Fatal(err)
}
return config
}

//GetClientID returns the Client ID of the config
func (c *Config) GetClientID() string {
return c.ClientID
}

//GetAuthority returns the authority URI of the config
func (c *Config) GetAuthority() string {
return c.Authority
}

//GetScopes returns all the scopes of the config
func (c *Config) GetScopes() []string {
return c.Scopes
}

//GetUsername returns the username of the config
func (c *Config) GetUsername() string {
return c.Username
}

//GetPassword returns the password of the config
func (c *Config) GetPassword() string {
return c.Password
}

//createPCAParams is used to instantiate the parameters to create the Public Client Application
func createPCAParams(clientID string, authority string) *msalgo.PublicClientApplicationParameters {
pcaParams := msalgo.CreatePublicClientApplicationParameters(clientID)
pcaParams.SetAadAuthority(authority)
return pcaParams
}
14 changes: 14 additions & 0 deletions src/examples/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
"os"
)

func main() {
exampleType := os.Args[1]
if exampleType == "1" {
acquireTokenDeviceCode()
} else if exampleType == "2" {
acquireByUsernamePasswordPublic()
}
}
Loading