-
Notifications
You must be signed in to change notification settings - Fork 103
Refactored and fixed device code sample #1
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
Merged
Merged
Changes from all 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 hidden or 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,48 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT license. | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "time" | ||
|
|
||
| msalgo "github.com/AzureAD/microsoft-authentication-library-for-go/src/msal" | ||
| 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()) | ||
| publicClientApp, err := msalgo.CreatePublicClientApplication(pcaParams) | ||
| if err != nil { | ||
| log.Fatal(err) | ||
| } | ||
| cancelCtx, cancelFunc := context.WithTimeout(context.Background(), time.Duration(cancelTimeout)*time.Second) | ||
| defer cancelFunc() | ||
| deviceCodeParams := msalgo.CreateAcquireTokenDeviceCodeParameters(cancelCtx, config.GetScopes(), deviceCodeCallback) | ||
| resultChannel := make(chan msalgo.IAuthenticationResult) | ||
| errChannel := make(chan error) | ||
| go func() { | ||
| result, err := publicClientApp.AcquireTokenByDeviceCode(deviceCodeParams) | ||
| errChannel <- err | ||
| resultChannel <- result | ||
| }() | ||
| err = <-errChannel | ||
| if err != nil { | ||
| log.Fatal(err) | ||
| } | ||
| result := <-resultChannel | ||
| fmt.Println("Access token is " + result.GetAccessToken()) | ||
| } |
This file contains hidden or 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 hidden or 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/msal" | ||
| 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 hidden or 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() | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,12 @@ | ||
| 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.5+incompatible | ||
| github.com/sirupsen/logrus v1.6.0 | ||
| github.com/twinj/uuid v1.0.0 | ||
| golang.org/x/sys v0.0.0-20200610111108-226ff32320da // indirect | ||
| ) |
This file contains hidden or 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,19 @@ | ||
| 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.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/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-20200610111108-226ff32320da h1:bGb80FudwxpeucJUjPYJXuJ8Hk91vNtfvrymzwiei38= | ||
| golang.org/x/sys v0.0.0-20200610111108-226ff32320da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.