Using Google OAUTH service to send an email using nodemailer. It works in the local environment (windows), but not in production (AlmaLinux OS).However, gave me an "504 XOAUTH2 authentication mechanism not supported" error in production environment.
Any suggestions would be greatly appreciated. Thanks
Code:
const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
type: "OAuth2",
user: "test#gmail.com",
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRECT,
refreshToken: process.env.GOOGLE_REFRESH_TOKEN,
accessToken: googleToken
},
tls: {
rejectUnauthorized: true
},
})
const mailData = {
from: "test#gmail.com",
to: emailDetails.to,
subject: emailDetails.subject,
// text: emailDetails.message,
html: `<div>${emailDetails.message}</div>
`
}
// console.log(JSON.stringify(mailData));
transporter.sendMail(mailData, function (err, info) {
if(err){
console.log(err)
res.send({status: 0, message:"Error occured", errors: [{code: "", message: err}]});
} else {
console.log(info)
res.send({status: 1, message:"successfully send an email", errors: []});
}
transporter.close();
})
}).catch(error => console.log(error));
][1]
Error message output here:
enter image description here
Related
I am trying to integrate a NextJS application with Okta, using the Authorization code flow with PKCE. The flow is not complete because the token request is not being performed.
This is the configuration for the provider:
import NextAuth from 'next-auth';
const oktaBaseUrl = 'https://my-okta-domain.com/oauth2/[auth-server-id]';
const clientId = '[My Client Id]';
const authorizationUrl =
oktaBaseUrl +
'/v1/authorize?response_type=code&response_mode=query&state=false';
const accessTokenUrl = oktaBaseUrl + '/v1/token';
const profileUrl = oktaBaseUrl + '/v1/userinfo';
export default NextAuth({
providers: [
{
id: 'okta',
name: 'Okta',
type: 'oauth',
version: '2.0',
protection: 'pkce',
clientId,
clientSecret: '',
accessTokenUrl,
authorizationUrl,
profileUrl,
scope: 'services',
params: {
grant_type: 'authorization_code',
},
profile(profile) {
return {
id: profile.id as string,
name: profile.name,
email: profile.email
};
}
}
],
});
Thefirst stage seems to be perfromed correctly, but when okta returns the code, I only see a message in my application showing an 403 code. It seems it is trying to get the code without perform the request to the token endpoint
Message in console:
[next-auth][error][oauth_get_access_token_error]
https://next-auth.js.org/errors#oauth_get_access_token_error { statusCode: 403, data: '' } okta y64EzO0u9ZbwqFdjJWqapXggDmC1bWx2DGQaITCpta4
[next-auth][error][oauth_callback_error]
https://next-auth.js.org/errors#oauth_callback_error { statusCode: 403, data: '' }
Is there a configuration I am missing?
I am trying to add custom oauth provider to my next.js app. I am adding custom provider in [...nextauth].js:
export default NextAuth({
// Configure one or more authentication providers
providers: [
{
id: "moneybutton",
name: "Money Button",
type: "oauth",
version: "2.0",
scope: "auth.user_identity:read users.profiles:read users.profiles.email:read users.balance:read",
params: {
grant_type: "authorization_code"
},
accessTokenUrl: "https://www.moneybutton.com/oauth/v1/token",
requestTokenUrl: "https://www.moneybutton.com/oauth/v1/token",
authorizationUrl: "https://www.moneybutton.com/oauth/v1/authorize?response_type=code",
profileUrl: "https://www.moneybutton.com/api/v1/auth/user_identity",
profile(profile) {
return {
id: profile.data.attributes.id,
name: profile.data.attributes.name,
};
},
clientId: 'my_oauth_identifier',
clientSecret: 'my_client_secret'
}
// ...add more providers here
],
debug: true
});
OAuth flow seems to work correct, as i am seeing my profile id coming back in responses but it finishes on http://localhost:3000/api/auth/signin?error=Callback
I set debug to true and i am getting following errors:
[next-auth][error][oauth_get_access_token_error]
https://next-auth.js.org/errors#oauth_get_access_token_error {
statusCode: 400,
data: '{"errors":[{"id":"6da534f0-a512-11eb-92e8-891975d02f44","status":400,"title":"Bad Request","detail":"Invalid client: client is invalid"}],"jsonapi":{"version":"1.0"}}'
} undefined undefined
[next-auth][error][oauth_get_access_token_error]
https://next-auth.js.org/errors#oauth_get_access_token_error {
statusCode: 400,
data: '{"errors":[{"id":"6da534f0-a512-11eb-92e8-891975d02f44","status":400,"title":"Bad Request","detail":"Invalid client: client is invalid"}],"jsonapi":{"version":"1.0"}}'
} moneybutton 9f3970b8ae39f9d46f9fae56f6fb6135ecb7e87b
[next-auth][error][oauth_callback_error]
https://next-auth.js.org/errors#oauth_callback_error {
statusCode: 400,
data: '{"errors":[{"id":"6da534f0-a512-11eb-92e8-891975d02f44","status":400,"title":"Bad Request","detail":"Invalid client: client is invalid"}],"jsonapi":{"version":"1.0"}}'
It says that client is invalid, but i am sure oauth identifier and secret are correct, as well as redirect URL set to http://localhost:3000/api/auth/callback/moneybutton.
response for profile looks like this if it would be helpful:
{
"data": {
"id": "75101",
"type": "user_identities",
"attributes": {
"id": "75101",
"name": "John Doe"
}
},
"jsonapi": {
"version": "1.0"
}
}
Links to documentations:
https://next-auth.js.org/configuration/providers
https://docs.moneybutton.com/docs/api/v1/api-v1-user-identity
I don't know if it's some bug or my approach is wrong and will apreciate any help
To send a silent remote notification to my iOS app from Google Firebase Functions, this is my index.js file:
const functions = require('firebase-functions');
const admin = require('firebase-admin')
admin.initializeApp();
const messaging = admin.messaging();
exports.sendPushNotification = functions.https.onRequest((req, res) => {
var payload = {
"notification": {
title: 'test title 2',
body: 'test body 2.1',
icon: 'https://i.pinimg.com/236x/2f/b9/bd/2fb9bd91b530d20410744249a418240b.jpg'
}
"data": {
"aps" = {
"content-available" : 1,
};
}
}
messaging.sendToTopic("backgroundUpdate", payload).then(function (response) {
console.log(response);
res.send(response);
}).catch(e => {
console.log(e)
res.send(e)
})
});
But Google keeps giving me this error message:
Deployment failure:
Build failed: /workspace/index.js:13
"data": { ^^^^^^ SyntaxError: Unexpected string at new Script (vm.js:83:7) at checkScriptSyntax (internal/bootstrap/node.js:620:5) at startup (internal/bootstrap/node.js:280:11) at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3); Error ID: d984e68f
Can anyone point me to my error? I have not been able to find a solution.
In my react website I login i want to generate a token. I'm calling my API and respone is undefined.
[object Error]{config: Object {...}, description: "Network Error",
message: "Network Error", name: "Error", request: XMLHttpRequest {...}, response: undefined, stack: "Error: Netw..."}
This is my react code.
const axios = require("axios");
const fakeAuth = {
isAuthenticated: false,
authenticate(cb: any) {
this.isAuthenticated = true;
axios.post('https://localhost:44310/api/Users/Token' )
.then((response : any) => {
console.log(response)
})
.catch((error : any) =>{
console.log(error)
})
},
signout(cb: any) {
this.isAuthenticated = false;
},
getValue() {
return this.isAuthenticated;
}
};
export default fakeAuth;
and this my API token method.
private string Token()
{
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("qwertyuiopqwertyuiop"));
var signInCred = new SigningCredentials(key, SecurityAlgorithms.HmacSha256Signature);
var token = new JwtSecurityToken(
issuer: "localhost",
audience: "localhost",
expires: DateTime.Now.AddMinutes(1),
signingCredentials: signInCred
);
var tokenString = new JwtSecurityTokenHandler().WriteToken(token);
return tokenString;
}
I have a node express application that I am attempting to add nodemail support. I have created a gmail account for this purpose (NOTE: the gmail account has "Less Secure App" enabled). My code is as follows:
var mailOptions = {
from: "XXXX#gmail.com",
to: YYYY#gmail.com,
subject: "Test",
generateTextFromHTML: true,
text: 'This is a test'
};
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
xoauth2: xoauth2.createXOAuth2Generator({
user: 'XXXX#gmail.com', // Your gmail address.
clientId: '123456789abcd-123456789abcdef.apps.googleusercontent.com',
clientSecret: 'XXXX-123456789',
refreshToken: '1/abcdefg',
accessToken: '123.abcdef'
})
}
});
transporter.sendMail(mailOptions, function(error, response) {
if (error) {
console.log('BAD' + error);
} else {
console.log('GOOD' + response);
}
transporter.close();
});
I am able to send e-mail some of the time. After a few successful attempts, I get the following error:
Error: connect ETIMEDOUT
I will continue to get these timeout errors and then it will work again for a few attempts.