-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from AzureAD/device-code-sample
Refactored and fixed device code sample
- Loading branch information
Showing
44 changed files
with
394 additions
and
252 deletions.
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
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 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/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 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 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,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 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 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
Oops, something went wrong.