microsoft: Make interface testable

Enable testing by allowing overriding the API host name in tests
This commit is contained in:
Carl Henrik Lunde 2020-01-13 08:15:07 +01:00
parent 3cbba11012
commit 5db29eb087

View File

@ -31,7 +31,6 @@ const (
) )
const ( const (
apiURL = "https://graph.microsoft.com"
// Microsoft requires this scope to access user's profile // Microsoft requires this scope to access user's profile
scopeUser = "user.read" scopeUser = "user.read"
// Microsoft requires this scope to list groups the user is a member of // Microsoft requires this scope to list groups the user is a member of
@ -54,6 +53,8 @@ type Config struct {
// Open returns a strategy for logging in through Microsoft. // Open returns a strategy for logging in through Microsoft.
func (c *Config) Open(id string, logger log.Logger) (connector.Connector, error) { func (c *Config) Open(id string, logger log.Logger) (connector.Connector, error) {
m := microsoftConnector{ m := microsoftConnector{
apiURL: "https://login.microsoftonline.com",
graphURL: "https://graph.microsoft.com",
redirectURI: c.RedirectURI, redirectURI: c.RedirectURI,
clientID: c.ClientID, clientID: c.ClientID,
clientSecret: c.ClientSecret, clientSecret: c.ClientSecret,
@ -94,6 +95,8 @@ var (
) )
type microsoftConnector struct { type microsoftConnector struct {
apiURL string
graphURL string
redirectURI string redirectURI string
clientID string clientID string
clientSecret string clientSecret string
@ -123,8 +126,8 @@ func (c *microsoftConnector) oauth2Config(scopes connector.Scopes) *oauth2.Confi
ClientID: c.clientID, ClientID: c.clientID,
ClientSecret: c.clientSecret, ClientSecret: c.clientSecret,
Endpoint: oauth2.Endpoint{ Endpoint: oauth2.Endpoint{
AuthURL: "https://login.microsoftonline.com/" + c.tenant + "/oauth2/v2.0/authorize", AuthURL: c.apiURL + "/" + c.tenant + "/oauth2/v2.0/authorize",
TokenURL: "https://login.microsoftonline.com/" + c.tenant + "/oauth2/v2.0/token", TokenURL: c.apiURL + "/" + c.tenant + "/oauth2/v2.0/token",
}, },
Scopes: microsoftScopes, Scopes: microsoftScopes,
RedirectURL: c.redirectURI, RedirectURL: c.redirectURI,
@ -296,7 +299,7 @@ type user struct {
func (c *microsoftConnector) user(ctx context.Context, client *http.Client) (u user, err error) { func (c *microsoftConnector) user(ctx context.Context, client *http.Client) (u user, err error) {
// https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/user_get // https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/user_get
req, err := http.NewRequest("GET", apiURL+"/v1.0/me?$select=id,displayName,userPrincipalName", nil) req, err := http.NewRequest("GET", c.graphURL+"/v1.0/me?$select=id,displayName,userPrincipalName", nil)
if err != nil { if err != nil {
return u, fmt.Errorf("new req: %v", err) return u, fmt.Errorf("new req: %v", err)
} }
@ -355,7 +358,7 @@ func (c *microsoftConnector) getGroupIDs(ctx context.Context, client *http.Clien
in := &struct { in := &struct {
SecurityEnabledOnly bool `json:"securityEnabledOnly"` SecurityEnabledOnly bool `json:"securityEnabledOnly"`
}{c.onlySecurityGroups} }{c.onlySecurityGroups}
reqURL := apiURL + "/v1.0/me/getMemberGroups" reqURL := c.graphURL + "/v1.0/me/getMemberGroups"
for { for {
var out []string var out []string
var next string var next string
@ -383,7 +386,7 @@ func (c *microsoftConnector) getGroupNames(ctx context.Context, client *http.Cli
IDs []string `json:"ids"` IDs []string `json:"ids"`
Types []string `json:"types"` Types []string `json:"types"`
}{ids, []string{"group"}} }{ids, []string{"group"}}
reqURL := apiURL + "/v1.0/directoryObjects/getByIds" reqURL := c.graphURL + "/v1.0/directoryObjects/getByIds"
for { for {
var out []group var out []group
var next string var next string