Sencha Touch 2 & Spring Security cross-domain login - spring-security

I need use API of some server from Sencha Touch 2 app . For using this API I need authenticate on server.
So I already implemented login functionality :
Ext.Ajax.request({
url: 'http://192.168.1.2:8080/spring-security-extjs-login/j_spring_security_check',
method: 'POST',
params: {
j_username: 'rod',
j_password: 'koala',
},
withCredentials: false,
useDefaultXhrHeader: false,
success: function(response){
var text = response.responseText;
Ext.Msg.alert("success", text, Ext.emptyFn);
},
failure: function(response){
var text = response.responseText;
Ext.Msg.alert('Error', text, Ext.emptyFn);
}
});
But how I can call API , because after authentication I try call API but they already want authentication. Probably I need save JSESSIONID and added it to another request, but I don't know how I can do it.
I can't use withCredentials: true , so I need to find another solution.
How I can get Set-Cookies from response HTTP Header ?
I see in Chrome console, that JSESSIONID present in response header , so , i need get it.
Please, help me find any solutions.

You can use requestcomplete & beforerequest events to read response headers and to write request headers respectively. Here is sample code :
Ext.Ajax.on('requestcomplete', function(conn, response, options, eOpts){
var respObj = Ext.JSON.decode(response.responseText);
Helper.setSessionId(options.headers['JSESSIONID']);
}, this);
Ext.Ajax.on('beforerequest', function(conn, options, eOptions){
options.headers['JSESSIONID'] = Helper.getSessionId();
}, this);

Related

Perform call to Microsoft Graph Api for Intune approveApps

I am working on automating Intune to perform the Managed Google Play Application approvals, the API documentation I have been referencing is here:
https://learn.microsoft.com/en-us/graph/api/intune-androidforwork-androidmanagedstoreaccountenterprisesettings-approveapps?view=graph-rest-beta
Requirements for approveApps is almost identical to syncApps:
https://learn.microsoft.com/en-us/graph/api/intune-androidforwork-androidmanagedstoreaccountenterprisesettings-syncapps?view=graph-rest-beta
I can make the call to syncApps successfully but approveApps returns BadRequest. The only difference between the calls appears to be the body requirements.
It needs packageIds as a String collection and approveAllPermissions as a Boolean.
Please help me to successfully make a post to https://graph.microsoft.com/beta/deviceManagement/androidManagedStoreAccountEnterpriseSettings/approveApps
Minimum Reproducible Code:
var authHeader = {
Authorization: `Bearer ${accessToken}`,
"Content-Type": "application/json"
};
var appApprovePostData = JSON.stringify({
packageIds: ["com.bundle.example"],
approveAllPermissions: true
});
var appApproveOptions = {
method: "POST",
uri:
"https://graph.microsoft.com/beta/deviceManagement/androidManagedStoreAccountEnterpriseSettings/approveApps",
headers: authHeader,
body: appApprovePostData
};
response = await request(appApproveOptions);
The application needs to be prefaced with "app:". So, in your example, you need
var appApprovePostData = JSON.stringify({
packageIds: ["app:com.bundle.example"],
approveAllPermissions: true
Couple of thoughts -
If you get back a RequestID, can you post that?
Can you compare the request body submitted by the Azure Portal (F12 developer mode to get the request body trace) for the same app approval with your request body generated from code?
Dave

What do I do with low Scores in reCAPTCHA v3?

I have set up reCAPTCHA v3 on my ASP.NET MVC project. Everything is working fine and is passing back data properly.
So the code below depends on another dll I have, but basically, the response is returned in the form of an object that shows everything that the JSON request passes back, as documented by https://developers.google.com/recaptcha/docs/v3
It all works.
But now that I know the response was successful, and I have a score, what do I do? What happens if the score is .3 or below? Some people recommend having v2 also set up for secondary validation (i.e. the 'choose all the stop signs in this picture' or 'type the word you see'). Is that really the only 'good' option?
Obviously the code isn't perfect yet. I'll probably handle the solution in the AJAX call rather than the controller, but still. What should I do if the score is low?
I read this article
reCaptcha v3 handle score callback
and it helped a little bit, but I'm still struggling to understand. I don't necessarily need code (although it would never hurt) but just suggestions on what to do.
VIEW:
<script src="https://www.google.com/recaptcha/api.js?render=#Session["reCAPTCHA"]"></script>
grecaptcha.ready(function () {
grecaptcha.execute('#Session["reCAPTCHA"]', { action: 'homepage' }).then(function (token) {
$.ajax({
type: "POST",
url: "Home/Method",
data: JSON.stringify({token: token }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
console.log('Passed the token successfully');
},
failure: function (response) {
alert(response.d);
}
});
});
});
CONTROLLER:
[HttpPost]
public void ReCaptchaValidator(string token)
{
ReCaptcha reCaptcha = new ReCaptcha();
Models.ReCaptcha response = new Models.ReCaptcha();
response = reCaptcha.ValidateCaptcha(token);
//response returns JSON object including sucess and score
if (response.Success)
{
//WHAT DO I DO HERE????
}
}
Ended up getting the answer from another forum. Basically, the answer is "anything you want". There is no right or wrong when handing a successful response.
So what could be done, is if the response is successful and CAPTCHA doesn't throw a flag, do nothing. But if CAPTCHA is unhappy, you could display an alert or a banner that says 'could not process', or you could even add in CAPTCA version 2, which would make them do the picture test or the 'I am not a robot' checkbox, etc.

Google Spreadsheet Protected Web-App Access Level Anyone

I have deployed a gsheets web-app and the code executes fine if the web-app's "who has access to this" is set to "anyone, annonymous" access. below is this code.
var data = {
'command1': 'usermenu',
'command2': 'dopayroll'
};
var options = {
'method' : 'post',
'payload' : data
};
var response = UrlFetchApp.fetch(ScriptApp.getService().getUrl(), options);
getSpread().toast(response.getContentText());
What I want to do, is change the access level to "Anyone" - which is access by any authenticated google account (right?). Added the following code to "var options" but returns an error code 401. How can I make this work?
'header' : { 'Authorization': 'Bearer ' + ScriptApp.getOAuthToken() },
EDIT; I should add that I'm a newbie in things related to OAuth and stuff.
There was a response posted which is now not visible.
My thanks to the person who did and having tested it, I can now confirm that the solution proposed works. There were 2 parts to the solution. First the correct header below ("header" should really be "headers")
'headers' : { 'Authorization': 'Bearer ' + ScriptApp.getOAuthToken() },
and adding the following comment to trigger Drive scope access for the project.
// DriveApp.getFiles()

How can I set a task to completed using the Asana API?

I'm wondering how to use the Asana APIs to send the command to complete a task.
I was trying something like:
https://app.asana.com/api/1.0/tasks/417207316735809/?opt_pretty&completed=true
but it doesn't work like that, I've checked the documentation, but couldn't find an answer.
Asana doc (someone requested it in comments): https://asana.com/developers/documentation/getting-started/input-output-options
Since I want to use it in my chrome extension I can't use curl so I treid ajax:
$.ajax({
type : "GET",
url : "https://app.asana.com/api/1.0/tasks/417207316174232/?opt_pretty",
data: {
completed: true,
},
success : function(result) {
result = JSON.stringify(result);
alert(result);
},
error : function(result) {
alert('xxx');
}
});
Any idea how to pass "completed=true" from doc in js?
If you are updating a resource you need to make a PUT request, not a GET request.
When I made this change, your AJAX request worked.

React-rails store POST request response

currently i used Ruby on Rails as my backend to serve API to my apps. And my front end i use React-rails gem (React.js) to get/post data and generate output. Both API and apps are on different server.
My problem here is, i cannot store response from POST request i made in react. How can i do so ? I am new in react btw.
What i want to do now is when user want to login the apps will POST request to API and get "auth_token" response. I want to store this "auth_token" response in header and access into dashboard. But i stuck now how can i store the auth_token ftw.
Below is my code for POST data :
var LoginBody = React.createClass(
{
handleClick() {
var username = this.refs.username.value;
var password = this.refs.password.value;
$.ajax({
url: 'http://localhost:3001/api/v1/users/central/signin?',
type: 'POST',
dataType: 'json',
data: { user_login:{email: username, password:password} },
success: (data) => {
this.setState({auth_token: data.auth_token});
}.bind(this),
});
},
getInitialState(){
return {
auth_token: []
}
},
If there's someone that could solve my problem and teach me how can i store in header in my apps i really appreciate it.
Just store them in local storage like:
localStorage.authToken = data.auth_token
And then whenever you need them in your future requests, just read as:
localStorage.authToken

Resources