Lab 9 Solution #1
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The goal of this lab is to look at how we can
authenticate API calls. As this is not a security course, we will
barely scratch the surface, but we will provide
many links for further reading.
Exercise 1: Configure PHPAPP
You will want to get up and running with the
sample phpapp available at
Follow the instructions in the
README.md
Exercise 2: Create a Client
Create a table to track clients and their tokens.
A client has (at least) a
name, atokenand somedataA few hints, you can create a default token using
md5(random()::text),and make
dataajsonbfield.Exercise 3: Create an API
Create a new PHP file to track our api (
public/api.php) with the following:Let's start our server.
And test our client. We will use Curl
as our client, but you can also use Postman.
The output should look similar to
HTTP/1.1 200 OK Host: localhost:4000 Date: Mon, 23 Mar 2020 00:50:02 GMT Connection: close X-Powered-By: PHP/7.3.9 Content-Type: application/json {"hello":"world"}Now change the API (api.php) to instead return all the headers
provided by the client. HINT: getallheaders
Let's add a custom header, and then we should it in our reply.
curl -i -H 'X-Men: Wolverine' http://localhost:4000/api.phpThe output should include our header
HTTP/1.1 200 OK Host: localhost:4000 Date: Mon, 23 Mar 2020 00:56:22 GMT Connection: close X-Powered-By: PHP/7.3.9 Content-Type: application/json {"Host":"localhost:4000","User-Agent":"curl\/7.64.1","Accept":"*\/*","X-Men":"Wolverine"}Using IMDB so some other source,
change the reply to be the actual that played the role in the X-Men.
For example,
curl -H 'X-Men: Wolverine' http://localhost:4000/api.phpShould now return
{"mutant":"Wolverine","name":"Logan"}If you don't know the mutant, such as
curl -H 'X-Men: Wolfie' http://localhost:4000/api.phpThen return
{"mutant":"Wolfie","name":"Unknown"}If you do not provide the expected header like below.
curl -i -H 'X-People: Wolfie' http://localhost:4000/api.phpThen return a
400error code and message like. Hint: takea look at http_response_code
and take a look at HTTP status codes.
Exercise 3: Authenticated API
We now have everything we need to create an authenticated API.
We will be using the
Authenticationheader to store our securetoken. For more resources on securing your application, take a look at:
Let's update our our API to allow allow requests from
professorcharlesxavier.The example below should continue to work.
Should return our response as expected. Note that we are provided
Bearer <token>. Hint: Look at explodeto split the string and list to capture them.
Anyone else should receive a
401 Reply. This example should fail
Should return
HTTP/1.1 401 Unauthorized Host: localhost:4000 Date: Mon, 23 Mar 2020 01:39:09 GMT Connection: close X-Powered-By: PHP/7.3.9 Content-Type: application/json {"error":"Invalid token.","token":"patrickstewart","type":"Bearer"}Exercise 3: Client Tokens
Instead of hard-coding the token to be
professorcharlesxavierlet's use our
clientstable to authenticate this API.Let us add two clients
And now only those two clients can access our API.
So the following call should work as expected
As the tokens are not hard-coded, we should also be able to make
a similar call.
Any requests from others will be denied with a
401 Reply. This example should continue to fail