Skip to content

Commit

Permalink
Rename package auth to msauth
Browse files Browse the repository at this point in the history
  • Loading branch information
yaegashi committed Nov 4, 2019
1 parent 656e3bc commit 87de0b2
Show file tree
Hide file tree
Showing 13 changed files with 383 additions and 261 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ heavily relying on C# and non-portable .NET Framework.

## Changes

- **2019-11-02: Library API breaking changes:** Since #1 gets merged every request method should take a ctx for the first arg.
You can pass a nil for it to use the default ctx kept in http.Client.
- **2019-11-02 Library API breaking changes:**
- [#1][PR1] every request method should take a ctx for the first arg
- [#2][PR2] package auth renamed to msauth

[PR1]: https://github.com/yaegashi/msgraph.go/pull/1
[PR2]: https://github.com/yaegashi/msgraph.go/pull/2

## Usage

Expand Down
22 changes: 0 additions & 22 deletions auth/client_credentials_grant.go

This file was deleted.

50 changes: 0 additions & 50 deletions auth/token.go

This file was deleted.

129 changes: 0 additions & 129 deletions auth/token_manager.go

This file was deleted.

24 changes: 13 additions & 11 deletions cmd/msgraph-me/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@ import (
"net/http"
"os"

"github.com/yaegashi/msgraph.go/auth"
"github.com/yaegashi/msgraph.go/jsonx"
"github.com/yaegashi/msgraph.go/msauth"
msgraph "github.com/yaegashi/msgraph.go/v1.0"
"golang.org/x/oauth2"
)

const (
defaultTenantID = "common"
defaultClientID = "45c7f99c-0a94-42ff-a6d8-a8d657229e8c"
defaultScope = "openid profile offline_access User.Read Files.Read"
tokenStorePath = "token_store.json"
tokenCachePath = "token_cache.json"
)

var defaultScopes = []string{"openid", "profile", "offline_access", "User.Read", "Files.Read"}

func dump(o interface{}) {
enc := jsonx.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
Expand All @@ -30,20 +32,20 @@ func dump(o interface{}) {

func main() {
var tenantID, clientID string
flag.StringVar(&tenantID, "tenant_id", defaultTenantID, "Tenant ID")
flag.StringVar(&clientID, "client_id", defaultClientID, "Client ID")
flag.StringVar(&tenantID, "tenant-id", defaultTenantID, "Tenant ID")
flag.StringVar(&clientID, "client-id", defaultClientID, "Client ID")
flag.Parse()

m := auth.NewTokenManager()
m.Load(tokenStorePath)
t, err := m.DeviceAuthorizationGrant(tenantID, clientID, defaultScope, nil)
ctx := context.Background()
m := msauth.NewManager()
m.LoadFile(tokenCachePath)
ts, err := m.DeviceAuthorizationGrant(ctx, tenantID, clientID, defaultScopes, nil)
if err != nil {
log.Fatal(err)
}
m.Save(tokenStorePath)
m.SaveFile(tokenCachePath)

ctx := context.Background()
httpClient := t.Client(ctx)
httpClient := oauth2.NewClient(ctx, ts)
graphClient := msgraph.NewClient(httpClient)

{
Expand Down
33 changes: 18 additions & 15 deletions cmd/msgraph-spoget/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,59 +10,62 @@ import (
"net/http"
"os"

"github.com/yaegashi/msgraph.go/auth"
"github.com/yaegashi/msgraph.go/jsonx"
"github.com/yaegashi/msgraph.go/msauth"
msgraph "github.com/yaegashi/msgraph.go/v1.0"
"golang.org/x/oauth2"
)

const (
defaultTenantID = "common"
defaultClientID = "45c7f99c-0a94-42ff-a6d8-a8d657229e8c"
defaultScope = "Sites.Read.All Files.Read.All"
)

var defaultScopes = []string{"Sites.Read.All", "Files.Read.All"}

// Config is serializable configuration
type Config struct {
TenantID string `json:"tenant_id,omitempty"`
ClientID string `json:"client_id,omitempty"`
ClientSecret string `json:"client_secret,omitempty"`
TokenStore string `json:"token_store,omitempty"`
TokenCache string `json:"token_cache,omitempty"`
}

// App is application
type App struct {
Config
URL string
Token *auth.Token
TokenSource oauth2.TokenSource
GraphClient *msgraph.GraphServiceRequestBuilder
}

// Authenticate performs OAuth2 authentication
func (app *App) Authenticate(ctx context.Context) error {
var err error
m := auth.NewTokenManager()
m := msauth.NewManager()
if app.ClientSecret == "" {
if app.TokenStore != "" {
if app.TokenCache != "" {
// ignore errors
m.Load(app.TokenStore)
m.LoadFile(app.TokenCache)
}
app.Token, err = m.DeviceAuthorizationGrant(app.TenantID, app.ClientID, defaultScope, nil)
app.TokenSource, err = m.DeviceAuthorizationGrant(ctx, app.TenantID, app.ClientID, defaultScopes, nil)
if err != nil {
return err
}
if app.TokenStore != "" {
err = m.Save(app.TokenStore)
if app.TokenCache != "" {
err = m.SaveFile(app.TokenCache)
if err != nil {
return err
}
}
} else {
app.Token, err = m.ClientCredentialsGrant(app.TenantID, app.ClientID, app.ClientSecret, auth.DefaultMSGraphScope)
scopes := []string{msauth.DefaultMSGraphScope}
app.TokenSource, err = m.ClientCredentialsGrant(ctx, app.TenantID, app.ClientID, app.ClientSecret, scopes)
if err != nil {
return err
}
}
app.GraphClient = msgraph.NewClient(app.Token.Client(ctx))
app.GraphClient = msgraph.NewClient(oauth2.NewClient(ctx, app.TokenSource))
return nil
}

Expand Down Expand Up @@ -117,7 +120,7 @@ func main() {
flag.StringVar(&app.TenantID, "tenant-id", "", "Tenant ID (default:"+defaultTenantID+")")
flag.StringVar(&app.ClientID, "client-id", "", "Client ID (default: "+defaultClientID+")")
flag.StringVar(&app.ClientSecret, "client-secret", "", "Client secret (for client credentials grant)")
flag.StringVar(&app.TokenStore, "token-store", "", "OAuth2 token store path")
flag.StringVar(&app.TokenCache, "token-cache", "", "OAuth2 token cache path")
flag.StringVar(&app.URL, "url", "", "URL")
flag.Parse()

Expand All @@ -144,8 +147,8 @@ func main() {
if app.ClientSecret == "" {
app.ClientSecret = cfg.ClientSecret
}
if app.TokenStore == "" {
app.TokenStore = cfg.TokenStore
if app.TokenCache == "" {
app.TokenCache = cfg.TokenCache
}

ctx := context.Background()
Expand Down
Loading

0 comments on commit 87de0b2

Please sign in to comment.