Skip to content
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

Fix Microsoft connector emailToLowercase for refresh token #3699

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions connector/microsoft/microsoft.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,10 @@ func (c *microsoftConnector) Refresh(ctx context.Context, s connector.Scopes, id
return identity, fmt.Errorf("microsoft: get user: %v", err)
}

if c.emailToLowercase {
user.Email = strings.ToLower(user.Email)
}

identity.Username = user.Name
identity.Email = user.Email

Expand Down
62 changes: 62 additions & 0 deletions connector/microsoft/microsoft_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package microsoft

import (
"context"
"encoding/json"
"fmt"
"net/http"
Expand Down Expand Up @@ -101,6 +102,67 @@ func TestUserIdentityFromGraphAPI(t *testing.T) {
expectEquals(t, len(identity.Groups), 0)
}

func TestHandleCallbackWithEmailToLowercase(t *testing.T) {
s := newTestServer(map[string]testResponse{
"/v1.0/me?$select=id,displayName,userPrincipalName": {
data: user{ID: "S56767889", Name: "Jane Doe", Email: "Jane.Doe@example.com"},
},
"/" + tenant + "/oauth2/v2.0/token": dummyToken,
})
defer s.Close()

req, _ := http.NewRequest("GET", s.URL, nil)

c := microsoftConnector{apiURL: s.URL, graphURL: s.URL, tenant: tenant, emailToLowercase: true}
identity, err := c.HandleCallback(connector.Scopes{Groups: false}, req)
expectNil(t, err)
expectEquals(t, identity.Username, "Jane Doe")
expectEquals(t, identity.UserID, "S56767889")
expectEquals(t, identity.PreferredUsername, "")
expectEquals(t, identity.Email, "jane.doe@example.com")
expectEquals(t, identity.EmailVerified, true)
expectEquals(t, len(identity.Groups), 0)
}

func TestRefreshWithEmailToLowercase(t *testing.T) {
s := newTestServer(map[string]testResponse{
"/v1.0/me?$select=id,displayName,userPrincipalName": {
data: user{ID: "S56767889", Name: "Jane Doe", Email: "Jane.Doe@example.com"},
},
"/" + tenant + "/oauth2/v2.0/token": {data: map[string]interface{}{
"access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9",
"refresh_token": "oRzxVjCnohYRHEYEhZshkmakKmoyVoTjfUGC",
"expires_in": "30",
}},
})
defer s.Close()

req, err := http.NewRequest("GET", s.URL, nil)
expectNil(t, err)

c := microsoftConnector{apiURL: s.URL, graphURL: s.URL, tenant: tenant, emailToLowercase: true}

expectNil(t, err)

identity, err := c.HandleCallback(connector.Scopes{Groups: false, OfflineAccess: true}, req)
expectNil(t, err)
expectEquals(t, identity.Username, "Jane Doe")
expectEquals(t, identity.UserID, "S56767889")
expectEquals(t, identity.PreferredUsername, "")
expectEquals(t, identity.Email, "jane.doe@example.com")
expectEquals(t, identity.EmailVerified, true)
expectEquals(t, len(identity.Groups), 0)

Copy link
Contributor Author

@hur hur Aug 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't add a check for expectEquals(t, identity.ConnectorData, expectedConnectorData) (like in gitea_test.go, for example) as I wasn't sure if there's a good way to mock the Expiry time from a quick look around the codebase

identity, err = c.Refresh(context.Background(), connector.Scopes{Groups: false, OfflineAccess: true}, identity)
expectNil(t, err)
expectEquals(t, identity.Username, "Jane Doe")
expectEquals(t, identity.UserID, "S56767889")
expectEquals(t, identity.PreferredUsername, "")
expectEquals(t, identity.Email, "jane.doe@example.com")
expectEquals(t, identity.EmailVerified, true)
expectEquals(t, len(identity.Groups), 0)
}

func TestUserGroupsFromGraphAPI(t *testing.T) {
s := newTestServer(map[string]testResponse{
"/v1.0/me?$select=id,displayName,userPrincipalName": {data: user{}},
Expand Down