2023-01-23 07:11:40 +00:00
|
|
|
package azuread
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/h2non/gock"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/zitadel/oidc/v2/pkg/oidc"
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
"golang.org/x/text/language"
|
|
|
|
|
2023-03-14 19:20:38 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2023-01-23 07:11:40 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/idp"
|
|
|
|
"github.com/zitadel/zitadel/internal/idp/providers/oauth"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSession_FetchUser(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
name string
|
|
|
|
clientID string
|
|
|
|
clientSecret string
|
|
|
|
redirectURI string
|
2023-03-15 06:48:37 +00:00
|
|
|
scopes []string
|
2023-01-23 07:11:40 +00:00
|
|
|
httpMock func()
|
|
|
|
options []ProviderOptions
|
|
|
|
authURL string
|
|
|
|
code string
|
2023-03-28 11:28:56 +00:00
|
|
|
tokens *oidc.Tokens[*oidc.IDTokenClaims]
|
2023-01-23 07:11:40 +00:00
|
|
|
}
|
|
|
|
type want struct {
|
|
|
|
err func(error) bool
|
|
|
|
user idp.User
|
|
|
|
id string
|
|
|
|
firstName string
|
|
|
|
lastName string
|
|
|
|
displayName string
|
|
|
|
nickName string
|
|
|
|
preferredUsername string
|
|
|
|
email string
|
|
|
|
isEmailVerified bool
|
|
|
|
phone string
|
|
|
|
isPhoneVerified bool
|
|
|
|
preferredLanguage language.Tag
|
|
|
|
avatarURL string
|
|
|
|
profile string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
want want
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "unauthenticated session, error",
|
|
|
|
fields: fields{
|
|
|
|
clientID: "clientID",
|
|
|
|
clientSecret: "clientSecret",
|
|
|
|
redirectURI: "redirectURI",
|
|
|
|
httpMock: func() {
|
|
|
|
gock.New("https://graph.microsoft.com").
|
2023-03-15 06:48:37 +00:00
|
|
|
Get("/v1.0/me").
|
2023-01-23 07:11:40 +00:00
|
|
|
Reply(200).
|
|
|
|
JSON(userinfo())
|
|
|
|
},
|
|
|
|
authURL: "https://login.microsoftonline.com/consumers/oauth2/v2.0/authorize?client_id=clientID&redirect_uri=redirectURI&response_type=code&scope=openid+profile+email&state=testState",
|
|
|
|
tokens: nil,
|
|
|
|
},
|
|
|
|
want: want{
|
|
|
|
err: func(err error) bool {
|
|
|
|
return errors.Is(err, oauth.ErrCodeMissing)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "user error",
|
|
|
|
fields: fields{
|
|
|
|
clientID: "clientID",
|
|
|
|
clientSecret: "clientSecret",
|
|
|
|
redirectURI: "redirectURI",
|
|
|
|
httpMock: func() {
|
|
|
|
gock.New("https://graph.microsoft.com").
|
2023-03-15 06:48:37 +00:00
|
|
|
Get("/v1.0/me").
|
2023-01-23 07:11:40 +00:00
|
|
|
Reply(http.StatusInternalServerError)
|
|
|
|
},
|
|
|
|
authURL: "https://login.microsoftonline.com/consumers/oauth2/v2.0/authorize?client_id=clientID&redirect_uri=redirectURI&response_type=code&scope=openid+profile+email&state=testState",
|
2023-03-28 11:28:56 +00:00
|
|
|
tokens: &oidc.Tokens[*oidc.IDTokenClaims]{
|
2023-01-23 07:11:40 +00:00
|
|
|
Token: &oauth2.Token{
|
|
|
|
AccessToken: "accessToken",
|
|
|
|
TokenType: oidc.BearerToken,
|
|
|
|
},
|
|
|
|
IDTokenClaims: oidc.NewIDTokenClaims(
|
|
|
|
"https://login.microsoftonline.com/consumers/oauth2/v2.0",
|
|
|
|
"sub2",
|
|
|
|
[]string{"clientID"},
|
|
|
|
time.Now().Add(1*time.Hour),
|
|
|
|
time.Now().Add(-1*time.Second),
|
|
|
|
"nonce",
|
|
|
|
"",
|
|
|
|
nil,
|
|
|
|
"clientID",
|
|
|
|
0,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: want{
|
|
|
|
err: func(err error) bool {
|
|
|
|
return err.Error() == "http status not ok: 500 Internal Server Error "
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "successful fetch",
|
|
|
|
fields: fields{
|
|
|
|
clientID: "clientID",
|
|
|
|
clientSecret: "clientSecret",
|
|
|
|
redirectURI: "redirectURI",
|
|
|
|
httpMock: func() {
|
|
|
|
gock.New("https://graph.microsoft.com").
|
2023-03-15 06:48:37 +00:00
|
|
|
Get("/v1.0/me").
|
2023-01-23 07:11:40 +00:00
|
|
|
Reply(200).
|
|
|
|
JSON(userinfo())
|
|
|
|
},
|
|
|
|
authURL: "https://login.microsoftonline.com/consumers/oauth2/v2.0/authorize?client_id=clientID&redirect_uri=redirectURI&response_type=code&scope=openid+profile+email&state=testState",
|
2023-03-28 11:28:56 +00:00
|
|
|
tokens: &oidc.Tokens[*oidc.IDTokenClaims]{
|
2023-01-23 07:11:40 +00:00
|
|
|
Token: &oauth2.Token{
|
|
|
|
AccessToken: "accessToken",
|
|
|
|
TokenType: oidc.BearerToken,
|
|
|
|
},
|
|
|
|
IDTokenClaims: oidc.NewIDTokenClaims(
|
|
|
|
"https://login.microsoftonline.com/consumers/oauth2/v2.0",
|
|
|
|
"sub",
|
|
|
|
[]string{"clientID"},
|
|
|
|
time.Now().Add(1*time.Hour),
|
|
|
|
time.Now().Add(-1*time.Second),
|
|
|
|
"nonce",
|
|
|
|
"",
|
|
|
|
nil,
|
|
|
|
"clientID",
|
|
|
|
0,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: want{
|
|
|
|
user: &User{
|
2023-03-15 06:48:37 +00:00
|
|
|
ID: "id",
|
|
|
|
BusinessPhones: []domain.PhoneNumber{"phone1", "phone2"},
|
|
|
|
DisplayName: "firstname lastname",
|
|
|
|
FirstName: "firstname",
|
|
|
|
JobTitle: "title",
|
2023-01-23 07:11:40 +00:00
|
|
|
Email: "email",
|
2023-03-15 06:48:37 +00:00
|
|
|
MobilePhone: "mobile",
|
|
|
|
OfficeLocation: "office",
|
|
|
|
PreferredLanguage: "en",
|
|
|
|
LastName: "lastname",
|
|
|
|
UserPrincipalName: "username",
|
2023-01-23 07:11:40 +00:00
|
|
|
isEmailVerified: false,
|
|
|
|
},
|
2023-03-15 06:48:37 +00:00
|
|
|
id: "id",
|
2023-01-23 07:11:40 +00:00
|
|
|
firstName: "firstname",
|
|
|
|
lastName: "lastname",
|
|
|
|
displayName: "firstname lastname",
|
|
|
|
nickName: "",
|
|
|
|
preferredUsername: "username",
|
|
|
|
email: "email",
|
|
|
|
isEmailVerified: false,
|
|
|
|
phone: "",
|
|
|
|
isPhoneVerified: false,
|
2023-03-15 06:48:37 +00:00
|
|
|
preferredLanguage: language.English,
|
2023-01-23 07:11:40 +00:00
|
|
|
profile: "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "successful fetch with email verified",
|
|
|
|
fields: fields{
|
|
|
|
clientID: "clientID",
|
|
|
|
clientSecret: "clientSecret",
|
|
|
|
redirectURI: "redirectURI",
|
|
|
|
options: []ProviderOptions{
|
|
|
|
WithEmailVerified(),
|
|
|
|
},
|
|
|
|
httpMock: func() {
|
|
|
|
gock.New("https://graph.microsoft.com").
|
2023-03-15 06:48:37 +00:00
|
|
|
Get("/v1.0/me").
|
2023-01-23 07:11:40 +00:00
|
|
|
Reply(200).
|
|
|
|
JSON(userinfo())
|
|
|
|
},
|
|
|
|
authURL: "https://login.microsoftonline.com/consumers/oauth2/v2.0/authorize?client_id=clientID&redirect_uri=redirectURI&response_type=code&scope=openid+profile+email&state=testState",
|
2023-03-28 11:28:56 +00:00
|
|
|
tokens: &oidc.Tokens[*oidc.IDTokenClaims]{
|
2023-01-23 07:11:40 +00:00
|
|
|
Token: &oauth2.Token{
|
|
|
|
AccessToken: "accessToken",
|
|
|
|
TokenType: oidc.BearerToken,
|
|
|
|
},
|
|
|
|
IDTokenClaims: oidc.NewIDTokenClaims(
|
|
|
|
"https://login.microsoftonline.com/consumers/oauth2/v2.0",
|
|
|
|
"sub",
|
|
|
|
[]string{"clientID"},
|
|
|
|
time.Now().Add(1*time.Hour),
|
|
|
|
time.Now().Add(-1*time.Second),
|
|
|
|
"nonce",
|
|
|
|
"",
|
|
|
|
nil,
|
|
|
|
"clientID",
|
|
|
|
0,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: want{
|
|
|
|
user: &User{
|
2023-03-15 06:48:37 +00:00
|
|
|
ID: "id",
|
|
|
|
BusinessPhones: []domain.PhoneNumber{"phone1", "phone2"},
|
|
|
|
DisplayName: "firstname lastname",
|
|
|
|
FirstName: "firstname",
|
|
|
|
JobTitle: "title",
|
2023-01-23 07:11:40 +00:00
|
|
|
Email: "email",
|
2023-03-15 06:48:37 +00:00
|
|
|
MobilePhone: "mobile",
|
|
|
|
OfficeLocation: "office",
|
|
|
|
PreferredLanguage: "en",
|
|
|
|
LastName: "lastname",
|
|
|
|
UserPrincipalName: "username",
|
2023-01-23 07:11:40 +00:00
|
|
|
isEmailVerified: true,
|
|
|
|
},
|
2023-03-15 06:48:37 +00:00
|
|
|
id: "id",
|
2023-01-23 07:11:40 +00:00
|
|
|
firstName: "firstname",
|
|
|
|
lastName: "lastname",
|
|
|
|
displayName: "firstname lastname",
|
|
|
|
nickName: "",
|
|
|
|
preferredUsername: "username",
|
|
|
|
email: "email",
|
|
|
|
isEmailVerified: true,
|
|
|
|
phone: "",
|
|
|
|
isPhoneVerified: false,
|
2023-03-15 06:48:37 +00:00
|
|
|
preferredLanguage: language.English,
|
2023-01-23 07:11:40 +00:00
|
|
|
profile: "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
defer gock.Off()
|
|
|
|
tt.fields.httpMock()
|
|
|
|
a := assert.New(t)
|
|
|
|
|
2023-03-15 06:48:37 +00:00
|
|
|
provider, err := New(tt.fields.name, tt.fields.clientID, tt.fields.clientSecret, tt.fields.redirectURI, tt.fields.scopes, tt.fields.options...)
|
2023-01-23 07:11:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
session := &oauth.Session{
|
|
|
|
AuthURL: tt.fields.authURL,
|
|
|
|
Code: tt.fields.code,
|
|
|
|
Tokens: tt.fields.tokens,
|
|
|
|
Provider: provider.Provider,
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := session.FetchUser(context.Background())
|
|
|
|
if tt.want.err != nil && !tt.want.err(err) {
|
|
|
|
a.Fail("invalid error", err)
|
|
|
|
}
|
|
|
|
if tt.want.err == nil {
|
|
|
|
a.NoError(err)
|
|
|
|
a.Equal(tt.want.user, user)
|
|
|
|
a.Equal(tt.want.id, user.GetID())
|
|
|
|
a.Equal(tt.want.firstName, user.GetFirstName())
|
|
|
|
a.Equal(tt.want.lastName, user.GetLastName())
|
|
|
|
a.Equal(tt.want.displayName, user.GetDisplayName())
|
|
|
|
a.Equal(tt.want.nickName, user.GetNickname())
|
|
|
|
a.Equal(tt.want.preferredUsername, user.GetPreferredUsername())
|
2023-03-14 19:20:38 +00:00
|
|
|
a.Equal(domain.EmailAddress(tt.want.email), user.GetEmail())
|
2023-01-23 07:11:40 +00:00
|
|
|
a.Equal(tt.want.isEmailVerified, user.IsEmailVerified())
|
2023-03-14 19:20:38 +00:00
|
|
|
a.Equal(domain.PhoneNumber(tt.want.phone), user.GetPhone())
|
2023-01-23 07:11:40 +00:00
|
|
|
a.Equal(tt.want.isPhoneVerified, user.IsPhoneVerified())
|
|
|
|
a.Equal(tt.want.preferredLanguage, user.GetPreferredLanguage())
|
|
|
|
a.Equal(tt.want.avatarURL, user.GetAvatarURL())
|
|
|
|
a.Equal(tt.want.profile, user.GetProfile())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-15 06:48:37 +00:00
|
|
|
func userinfo() *User {
|
|
|
|
return &User{
|
|
|
|
ID: "id",
|
|
|
|
BusinessPhones: []domain.PhoneNumber{"phone1", "phone2"},
|
|
|
|
DisplayName: "firstname lastname",
|
|
|
|
FirstName: "firstname",
|
|
|
|
JobTitle: "title",
|
|
|
|
Email: "email",
|
|
|
|
MobilePhone: "mobile",
|
|
|
|
OfficeLocation: "office",
|
|
|
|
PreferredLanguage: "en",
|
|
|
|
LastName: "lastname",
|
|
|
|
UserPrincipalName: "username",
|
|
|
|
}
|
2023-01-23 07:11:40 +00:00
|
|
|
}
|