I am bulding an app with an AngularJS frontend and a Flask REST API for the backend.
I am trying to authenticate a resource using AngularJS $http. My Flask backend is very simple and setup with this decorator to handle the CORS.
My basic view code in Flask looks like:
@auth.verify_password
def verify_password(username, password):
# for now, I just want to see the correct credentials are present
print 'credentials: %s %s' % (username, password)
return True
@app.route('/login')
@cross_origin('http:/localhost')
@auth.login_required
def index():
return jsonify({'auth': str(request.authorization)})
This works in my local dev environment, so that when I do
curl -u myusername:mypassword http://127.0.0.1:5000/login
I get the expected result:
* About to connect() to 127.0.0.1 port 5000 (#0)
* Trying 127.0.0.1...
* connected
* Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0)
* Server auth using Basic with user 'myusername'
> GET /login HTTP/1.1
> Authorization: Basic am9objpoZWxsbw==
> User-Agent: curl/7.26.0
> Host: 127.0.0.1:5000
> Accept: */*
>
* additional stuff not fine transfer.c:1037: 0 0
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Content-Type: application/json
< Content-Length: 20
< Access-Control-Allow-Origin: http://localhost
< Access-Control-Allow-Methods: HEAD, OPTIONS, GET
< Access-Control-Max-Age: 21600
< Access-Control-Allow-Credentials: true
< Access-Control-Allow-Headers: authorization
< Vary: Origin
< Server: Werkzeug/0.10.1 Python/2.7.3
< Date: Mon, 09 Mar 2015 17:37:27 GMT
{
"auth": "{'username': 'myusername', 'password': 'mypass'}"
* Closing connection #0
}
In my AngularJS app I want to do the same, so I have an authentication service:
myApp
.service('AuthService', function ($http, $rootScope, $state) {
return {
login: function (credentials) {
console.log('credentials', credentials); // credentials are correct
return $http
.get('http://127.0.0.1:5000/login', credentials)
.then(function (result) {
// do something
console.log('result', result);
});
},
...
}
})
I also configure $http:
// CORS
$httpProvider.defaults.useXDomain = true;
$httpProvider.defaults.withCredentials = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
$httpProvider.defaults.headers.common["Accept"] = "application/json";
$httpProvider.defaults.headers.common["Content-Type"] = "application/json";
When I examine the request details when using curl I can clearly see the username and password are passed. However, when using AngularJS the request.authorization object in Flask is empty, meaning there are no credentials or they are in the wrong place.
How do I pass the credentials correctly in AngularJS?
$http.defaults.headers.common.Authorization. Should it contain both the username and password?