|
| 1 | +package oidc_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "net/url" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/jarcoal/httpmock" |
| 11 | + |
| 12 | + "github.com/ory/kratos/internal" |
| 13 | + "github.com/ory/kratos/selfservice/strategy/oidc" |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | + "golang.org/x/oauth2" |
| 17 | +) |
| 18 | + |
| 19 | +const fakeIDToken = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJleHAiOjk5OTk5OTk5OTksImF1ZCI6ImFiY2QiLCJpc3MiOiJodHRwczovL3Jhdy5naXRodWJ1c2VyY29udGVudC5jb20vYWVuZWFzci9wcml2YXRlLW9pZGMvbWFzdGVyL3Rva2VuIn0.G9v8pJXJrEOgdJ5ecE6sIIcTH_p-RKkBaImfZY5DDVCl7h5GEis1n3GKKYbL_O3fj8Fu-WzI2mquI8S8BOVCQ6wN0XtrqJv22iX_nzeVHc4V_JWV1q7hg2gPpoFFcnF3KKtxZLvDOA8ujsDbAXmoBu0fEBdwCN56xLOOKQDzULyfijuAa8hrCwespZ9HaqcHzD3iHf_Utd4nHqlTM-6upWpKIMkplS_NGcxrfIRIWusZ0wob6ryy8jECD9QeZpdTGUozq-YM64lZfMOZzuLuqichH_PCMKFyB_tOZb6lDIiiSX4Irz7_YF-DP-LmfxgIW4934RqTCeFGGIP64h4xAA" |
| 20 | + |
| 21 | +func TestProviderFacebook_Claims(t *testing.T) { |
| 22 | + httpmock.Activate() |
| 23 | + defer httpmock.DeactivateAndReset() |
| 24 | + |
| 25 | + httpmock.RegisterResponder("GET", "https://graph.facebook.com/me", |
| 26 | + func(req *http.Request) (*http.Response, error) { |
| 27 | + if _, ok := req.URL.Query()["appsecret_proof"]; !ok { |
| 28 | + resp, err := httpmock.NewJsonResponse(400, map[string]interface{}{ |
| 29 | + "error": map[string]interface{}{ |
| 30 | + "message": "API calls from the server require an appsecret_proof argument", |
| 31 | + "type": "GraphMethodException", |
| 32 | + "code": 100, |
| 33 | + "fbtrace_id": "Ay8LR3n5BsHm809VYpJ3eDM", |
| 34 | + }, |
| 35 | + }) |
| 36 | + return resp, err |
| 37 | + } |
| 38 | + resp, err := httpmock.NewJsonResponse(200, map[string]interface{}{ |
| 39 | + "id": "123456789012345", |
| 40 | + "name": "John Doe", |
| 41 | + "first_name": "John", |
| 42 | + "last_name": "Doe", |
| 43 | + |
| 44 | + "birthday": "01/01/1990", |
| 45 | + }) |
| 46 | + return resp, err |
| 47 | + }, |
| 48 | + ) |
| 49 | + |
| 50 | + httpmock.RegisterResponder("GET", "https://www.facebook.com/.well-known/openid-configuration", |
| 51 | + func(req *http.Request) (*http.Response, error) { |
| 52 | + resp, err := httpmock.NewJsonResponse(200, map[string]interface{}{ |
| 53 | + "issuer": "https://www.facebook.com", |
| 54 | + }) |
| 55 | + return resp, err |
| 56 | + }, |
| 57 | + ) |
| 58 | + |
| 59 | + _, reg := internal.NewFastRegistryWithMocks(t) |
| 60 | + c := &oidc.Configuration{ |
| 61 | + ID: "facebook", |
| 62 | + Provider: "facebook", |
| 63 | + ClientID: "abcd", |
| 64 | + ClientSecret: "secret", |
| 65 | + Mapper: "file://./stub/oidc.facebook.jsonnet", |
| 66 | + Scope: []string{"email"}, |
| 67 | + } |
| 68 | + facebook := oidc.NewProviderFacebook(c, reg) |
| 69 | + |
| 70 | + actual, err := facebook.Claims( |
| 71 | + context.Background(), |
| 72 | + (&oauth2.Token{AccessToken: "foo", Expiry: time.Now().Add(time.Hour)}).WithExtra(map[string]interface{}{"id_token": fakeIDToken}), |
| 73 | + url.Values{}, |
| 74 | + ) |
| 75 | + require.NoError(t, err) |
| 76 | + |
| 77 | + assert.Equal(t, &oidc.Claims{ |
| 78 | + Issuer: "https://graph.facebook.com/me?fields=id,name,first_name,last_name,middle_name,email,picture,birthday,gender&appsecret_proof=773ba44693c7553d6ee20f61ea5d2757a9a4f4a44d2841ae4e95b52e4cd62db4", |
| 79 | + Subject: "123456789012345", |
| 80 | + Name: "John Doe", |
| 81 | + GivenName: "John", |
| 82 | + FamilyName: "Doe", |
| 83 | + Nickname: "John Doe", |
| 84 | + PreferredUsername: "John Doe", |
| 85 | + |
| 86 | + EmailVerified: true, |
| 87 | + Birthdate: "01/01/1990", |
| 88 | + }, actual) |
| 89 | +} |
0 commit comments