4

Premièrement, je ne suis pas un développeur JS chevronné, alors excusez les erreurs évidentes que j'aurais pu commettre. J'essaie de mettre en œuvre un authentificateur personnalisé pour authentifier un utilisateur avec Keycloak en utilisant l'OAuth2 Password Grant qui nécessite que le client_id soit passé dans le corps de la demande.Implémentation d'un authentificateur custom-authent-ember

import OAuth2PasswordGrant from 'ember-simple-auth/authenticators/oauth2-password-grant'; 
export default OAuth2PasswordGrant.extend({ 
    serverTokenEndpoint: 'http://localhost:8080/something/token', 
    makeRequest(url, data, headers = {}) { 
    data.client_id = 'my-app'; 
    return this._super(url, data, headers); 
    } 
}); 

J'ai un contrôleur qui utilise ce Authenticator en appelant cette action:

actions: { 
    authenticate() { 
    let {username, password} = this.getProperties('username', 'password'); 
    this.get('session').authenticate('authenticator:oauth2', username, password).then(() => { 
     // Do something 
    }).catch((response) => { 
     // Show error 
    }); 
    } 
} 

Cela provoque Firefox pour accrocher et me donne un message de script ne répond pas.

Si je retire la return de la méthode makeRequest(), je peux voir dans le débogueur du navigateur que l'appel à Keycloak retourne en fait correctement avec l'objet qui contient mon jeton etc. inspecteur Ember Cependant montre quelques erreurs liées à des promesses non résolues. Mais je suppose que c'est parce que je ne rends plus la promesse.

Qu'est-ce que je fais mal ici?

Comment puis-je résoudre le problème de script qui ne répond pas?

Y a-t-il un autre moyen pour moi d'atteindre mon objectif?

Edit 1: C'est quand je supprimer le retour

Voici l'objet réel qui est retourné:

{ 
"access_token":"eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJTRUNSd09fMlZWdGhxUVBUWnFxNHlqX0tKekxnOElSTjBrQkx5UTlacklrIn0.eyJqdGkiOiI1NDgzZDdkMi0zMDdhLTQyZjItYWUxZC0xYTZjMTZjOTM2ZjAiLCJleHAiOjE1MDgzMzE5MjAsIm5iZiI6MCwiaWF0IjoxNTA4MzMxNjIwLCJpc3MiOiJodHRwOi8vbG9jYWxob3N0OjgwODAvYXV0aC9yZWFsbXMvc2Z4LWl0cmF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tHej5RKHNI2wTzwK3IWHbkLWNndxSk_Lzj2-lCx380VpTkVpiDJfq5umjskOmI13dyPF7paA", 
"token_type":"bearer", 
"not-before-policy":0, 
"session_state":"40831eaa-4f2a-496e-945d-7beb17e46e44" 
} 

C'est ce que l'inspecteur de Ember (promesses) montre:

enter image description here

Voici la pile de la promesse:

Ember Inspector ($E): authenticate/<@http://localhost:4200/assets/vendor.js:77927:9 
[email protected]://localhost:4200/assets/vendor.js:63591:7 
[email protected]://localhost:4200/assets/vendor.js:64067:35 
[email protected]://localhost:4200/assets/vendor.js:77919:14 
[email protected]://localhost:4200/assets/vendor.js:78528:14 
[email protected]://localhost:4200/assets/vendor.js:79420:14 
[email protected]://localhost:4200/assets/sfx-itransfer-web-agent.js:855:9 
[email protected]://localhost:4200/assets/vendor.js:20249:24 
[email protected]://localhost:4200/assets/vendor.js:37657:12 
makeClosureAction/</<@http://localhost:4200/assets/vendor.js:29073:16 
[email protected]://localhost:4200/assets/vendor.js:37087:14 
makeClosureAction/<@http://localhost:4200/assets/vendor.js:29072:15 
submit/<@http://localhost:4200/assets/vendor.js:70453:20 
[email protected]://localhost:4200/assets/vendor.js:63549:14 
[email protected]://localhost:4200/assets/vendor.js:63562:15 
[email protected]://localhost:4200/assets/vendor.js:63532:9 
@http://localhost:4200/assets/vendor.js:54458:16 
[email protected]://localhost:4200/assets/vendor.js:19948:17 
[email protected]://localhost:4200/assets/vendor.js:19827:25 
[email protected]://localhost:4200/assets/vendor.js:20019:25 
[email protected]://localhost:4200/assets/vendor.js:20128:26 
[email protected]://localhost:4200/assets/vendor.js:20212:21 
[email protected]://localhost:4200/assets/vendor.js:20219:24 
[email protected]://localhost:4200/assets/vendor.js:37657:12 
handleEvent/<@http://localhost:4200/assets/vendor.js:58233:18 
[email protected]://localhost:4200/assets/vendor.js:37087:14 
[email protected]://localhost:4200/assets/vendor.js:58232:17 
[email protected]://localhost:4200/assets/vendor.js:57385:12 
[email protected]://localhost:4200/assets/vendor.js:57685:14 
setupHandler/<@http://localhost:4200/assets/vendor.js:57619:20 
[email protected]://localhost:4200/assets/vendor.js:5546:16 
add/[email protected]://localhost:4200/assets/vendor.js:5355:6 
+0

Je ne vois pas un bug dans le code que vous avez fourni. Vous devrez peut-être publier la sortie du message d'erreur. Le fait que Firefox soit suspendu est étrange, il doit y avoir une boucle infinie ou quelque chose de similaire se produisant - les promesses en elles-mêmes ne provoquent pas cela. – AlexMA

+0

C'est assez étrange. J'ai ajouté d'autres informations de débogage à la question – KernelKoder

+0

pourquoi ne pas définir 'clientId' comme si' serverTokenEndpoint' est défini? – user1156168

Répondre

0

En fait, votre solution semble correcte.

Je suppose que vous avez un problème dans la réponse du serveur ou des méthodes de demande désadaptation GET/POST . Pour résoudre cela, essayez de déboguer la promesse à l'intérieur makeRequest.

return new RSVP.Promise((resolve, reject) => { 
    fetch(url, options).then((response) => { 
    response.text().then((text) => { //<-- here debug text 
     let json = text ? JSON.parse(text) : {}; 
     if (!response.ok) { //<-- and here debug response 
     response.responseJSON = json; 
     reject(response); 
     } else { 
     resolve(json); 
     } 
    }); 
    }).catch(reject); 

Donc, si problème ici, juste récrire méthode tout makeRequest et ajouter vos propres promesses avec la coutume fetch.

Une autre façon est d'écrire sur mesure Authenticator, remplaçant authenticate, restore et (éventuellement) invalidate méthodes comme écrit dans la documentation: https://github.com/simplabs/ember-simple-auth#implementing-a-custom-authenticator

+0

J'ai donc En fait, j'ai essayé de réécrire toute la méthode avec ma propre promesse. Même résultat Je ne pense pas qu'il y ait un problème avec la demande/réponse car il retourne correctement, je peux voir l'objet retourné par le serveur. Ma prochaine étape serait d'écrire un 'Authenticator 'personnalisé, je suppose. – KernelKoder

+0

Etes-vous sûr que la section 'else' avec' resolve (json) 'du code ci-dessus est vraiment exécutée et contient' json' vous pouvez voir l'objet ici ou dans le débogueur du navigateur? – user1156168