-
Notifications
You must be signed in to change notification settings - Fork 89
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
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a29f377
Refactored and fixed device code sample
739e2f0
Addressed concerns in PR
b7db837
Moved variables to config and added readMe
b589d7b
Updated Device Code Flow to handle cancellation and returns device co…
3b0219b
New Device Code Flow
4b9b5a6
Updating Requests
c57ccfc
Added device code callback
071d861
Update sample readme
df99c12
Addressing comments in PR
b683bc2
Update IWebRequestManager.go
hchittanuru3 89a47b9
Added contexts to signal canceling
12ebd17
Refactored
5af4868
Merge branch 'device-code-sample' of https://github.com/AzureAD/micro…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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 aDeviceCodeResult
, and to make it accessible to the user, we utilizeIDeviceCodeResult
.There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?