Sign In with Google - Console.log - google-signin

I'm trying to use this new example for the new Sign In with Google since oAuth will be retired in 2023. Question is, how do I pass this value console.log("Email: " + responsePayload.email); to a hidden html field ? I was able to do this with the current oAuth Java funstion but can't seem to pass the value with this new version of the code.
decodeJwtResponse() is a custom function defined by you - could be what I'm missing. I would like to keep this entire function in java script if possible. I am lost
<div id="g_id_onload"
data-client_id="YOUR_GOOGLE_CLIENT_ID"
data-callback="handleCredentialResponse">
</div>
<script>
function handleCredentialResponse(response) {
// decodeJwtResponse() is a custom function defined by you
// to decode the credential response.
const responsePayload = decodeJwtResponse(response.credential);
console.log("ID: " + responsePayload.sub);
console.log('Full Name: ' + responsePayload.name);
console.log('Given Name: ' + responsePayload.given_name);
console.log('Family Name: ' + responsePayload.family_name);
console.log("Image URL: " + responsePayload.picture);
console.log("Email: " + responsePayload.email);
}
</script>

To decode the token you need to Base64 URL decode the message and parse it back into an object with JSON.parse, which is what jwt-decode does.
If you're not using a bundler with NPM you can import it:
console.log(jwt_decode('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c'))
<script src="https://unpkg.com/jwt-decode/build/jwt-decode.js"></script>
Or as an ES6 module:
<script type="module">
// Or https://unpkg.com/jwt-decode/build/jwt-decode.esm.js
import jwtDecode from 'https://esm.run/jwt-decode';
console.log(jwtDecode('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c'))
</script>

I was able to do it manually using https://jwt.io/ with algorithm RS256
And programmatically using jose library https://github.com/panva/jose
import * as jose from 'jose'
const claims = jose.decodeJwt(token)
console.log(claims)
The result is the same as in the documentation https://developers.google.com/identity/gsi/web/reference/js-reference#credential
You should verify the JWT (public keys, issuer, audience, issuer and expiration date), jose has a function to decode and verify called jwtVerify
https://github.com/panva/jose/blob/main/docs/functions/jwt_verify.jwtVerify.md#readme

Related

Google Sign In : Decoding JWT response

I have applied Google Sign In code; Everything is properly working. Now want to decode the jwt response. Can you please suggest how this decodeJwtResponse is formed.
Please Help.
function handleCredentialResponse(response) {
const responsePayload = decodeJwtResponse(response.credential);
console.log("ID: " + responsePayload.sub);
console.log('Full Name: ' + responsePayload.name);
console.log('Given Name: ' + responsePayload.given_name);
console.log('Family Name: ' + responsePayload.family_name);
console.log("Image URL: " + responsePayload.picture);
console.log("Email: " + responsePayload.email);
}
you can use jtw.io to decode the token and looks to everything included in the response (claims/audience/etc...)
Download the appropriate parsing mechanism form jwt.io. it has decoders for nodejs, .net, and more and you did not specify what you are using. Also note that you want to send the non-decoded version to the Google API for validation, the next and important step in the document I believe you obtained that code from.
You can use the jsonwebtoken library
After installing with npm i jsonwebtoken
import jwt from "jsonwebtoken";
function decodeJwtResponse(credential){
return jwt.decode(credentail)
}
function handleCredentialResponse(response) {
const responsePayload = decodeJwtResponse(response.credential);
console.log("ID: " + responsePayload.sub);
console.log('Full Name: ' + responsePayload.name);
console.log('Given Name: ' + responsePayload.given_name);
console.log('Family Name: ' + responsePayload.family_name);
console.log("Image URL: " + responsePayload.picture);
console.log("Email: " + responsePayload.email);
}

Using Graph in Outlook addin to read email in MIME format

I am getting lost with Outlook addin development and really need some help.
I have developed an addin that sends selected email to another server via REST API and it worked fine, but there was a limitation to 1MB so I tried to develop a solution that use ewsURL + SOAP but faced with CORS issues.
Now I got a suggestion to use GRAPH approach (fine with me) but I have no idea how that suppose to work using JavaScript.
Basically I need to get an email as MIME/EML format.
I was guided to check this article: https://learn.microsoft.com/en-us/graph/outlook-get-mime-message
There is endpoint that looks promissing:
https://graph.microsoft.com/v1.0/me/messages/4aade2547798441eab5188a7a2436bc1/$value
But I do not see explanation
how to make authorization process?
I have tried to get token from getCallbackTokenAsync but that did not work
I have tried Office.context.auth.getAccessTokenAsync but getting an issue:
Error code: 13000 Error name: API Not Supported.
Error message: The identity API is not supported for this add-in.
how to get email id
I have tried to do Office.context.mailbox.item.itemId but it looks different compare to what I have seen in the examples (but hopefully that is not a problem)
Please help :-)
There are 2 solutions here. It is preferred longer term to use graph end point with https://learn.microsoft.com/en-us/office/dev/add-ins/develop/authorize-to-microsoft-graph and you can use https://graph.microsoft.com/v1.0/me/messages/4aade2547798441eab5188a7a2436bc1/$value. However this solution requires a backend / service . Transferring through backend is preferable for large content so the content can transfer directly from Exchange to the service.
Alternatively, you can get token from getCallbackTokenAsync, from this doc: https://learn.microsoft.com/en-us/office/dev/add-ins/outlook/use-rest-api
As you noted is that you will need to translate the ews id using convertToRestId. Putting together, your solution should look something like this:
Office.context.mailbox.getCallbackTokenAsync({isRest: true}, function(result){
if (result.status === "succeeded") {
let token = result.value;
var ewsItemId = Office.context.mailbox.item.itemId;
const itemId = Office.context.mailbox.convertToRestId(
ewsItemId,
Office.MailboxEnums.RestVersion.v2_0);
// Request the message's attachment info
var getMessageUrl = Office.context.mailbox.restUrl +
'/v2.0/me/messages/' + itemId + '/$value';
var xhr = new XMLHttpRequest();
xhr.open('GET', getMessageUrl);
xhr.setRequestHeader("Authorization", "Bearer " + token);
xhr.onload = function (e) {
console.log(this.response);
}
xhr.onerror = function (e) {
console.log("error occurred");
}
xhr.send();
}
});

Karate - Authentication - cannot access url address under password

Using Karate, I have need to use basic authentication (to pass common authentication dialog window with username and password), and I have tried this: https://github.com/intuit/karate#http-basic-authentication-example).
I have created the file basic-auth.js
function fn(creds) {
var temp = creds.username + ':' + creds.password;
var Base64 = Java.type('java.util.Base64');
var encoded = Base64.getEncoder().encodeToString(temp.bytes);
return 'Basic ' + encoded;
}
I have added the call to the test feature file I run (added to Scenario section):
header Authorization = call read('basic-auth.js') { username: 'realusernamestring', password: 'realpasswordstring' }
Then I have placed the url I want to access right after:
driver urlUnderPassword
But it did not work, I still cannot access the page. I think there is something missing, something what needs to be done. Could you help me what the problem might be?
Thank you.
What you are referring to is for API tests not UI tests.
If you need the browser / driver to do basic auth it should be easy, just put it in the URL: https://intellipaat.com/community/10343/http-basic-authentication-url-with-in-password
So I am guessing something like this will work:
* driver 'http://' + username + ':' + password + '#' + urlUnderPassword

send email in Meteor app with mailGun - Error invoking Method 'sendEmail'

I use Meteor v1 to buid un app, and to be able to send a email from my app, I add Email package.
This is my code on the client
Template.Home.events({
'click button': function(event, template){
event.preventDefault();
var depart = template.find('[id=exampleInputEmail1]').value;
var arrive = template.find('[id=exampleInputPassword1]').value;
var email = template.find('[id=exampleInputPassword1m]').value;
var nom = template.find('[id=exampleInputPassword1s]').value;
var telephone = template.find('[id=exampleInputPassword1n]').value;
var element = template.find('[id=exampleInputPassword1j]').value;
Meteor.call('sendEmail', 'nwabdou85#yahoo.fr', email, 'Faites moi un devis rapide svp', 'This is a test of Email.send.');
}
});
And the server one is
Meteor.startup(function() {
var username = "postmaster%40sandboxxxxxxxx.mailgun.org";
var password = "xxxxxxxxxxx";
var server = "smtp.mailgun.org";
var port = "587"
process.env.MAIL_URL = 'smtp://' + encodeURIComponent(username) + ':' + encodeURIComponent(password) + '#' + encodeURIComponent(server) + ':' + port;
});
// In your server code: define a method that the client can call
Meteor.methods({
'sendEmail': function (to, from, subject, text) {
// check([to, from, subject, text], [String]);
this.unblock();
Email.send({
to: to,
from: from,
subject: subject,
text: text
});
}
});
but it does not work !! it throw out this error on consol : Error invoking Method 'sendEmail': Internal server error [500]
Can you even have this issue and hwo do you fixe it ??
I would suggest switching your hosting to Heroku, which is free but more configurable. Try reading my recent article on the subject, should give you some hints: http://joshowens.me/modulus-vs-heroku-vs-digital-ocean/.
If that is a 500 error from mailgun then its something on their side not yours. try accessing your dashboard on mailgun and see if you can get some info from there. I spent abit of time trying to get this right and it was all to do with getting the Mail url correct.
I remember in the bulletproof meteor you should use Meteor.defer function to delay your email sending process. Usually sending email caused response timeout.
Again, this.unblock might not useful in this case. Please try to comment out it if the first way doesn't work.
encodeURIcomponent would convert something from sam#sam.com to sam%40sam.com.
Your username is already URI encoded, so doesn't need to be further encoded. In this case, you are double encoding the username, and hence was probably failing with an authentication error.

'#' in URL, used in node.js http.request

I'm presented with an url with an "#" sign in it:
curl http://subdomain:token#localhost:9292/aaa/bbb
works perfectly
But I can't get it to work with node.js http.request, probably because I don't understand what the "#" is doing (and somehow can't find a clear answer on google).
Anyone care to explain?
Here's my current node.js code
var http_options = {
method : "GET"
, url : subdomain + ":" + token + "#" + Config.API.url
, path : "/aaa/bbb"
};
var req = http.request(http_options, function (response) {
// ...
});
req.on('error', function (error) {
console.log('error: ' + error);
});
which produces:
error: ECONNREFUSED
The # is dividing the user / password part from the location part.
the curl line you wrote send a HTTP Authenticate (BASIC authentication) with the request.
curl http://subdomain:token#localhost:9292/aaa/bbb
means: Get localhost:9292/aaa/bbb and do it as user: subdomain password token
I have no idea how to do that in node.js, but you'll figure it out, now that you know what it does.

Resources