38
API Design & Security in Django Tareque Hossain Education Technology 1

API Design & Security in django

Embed Size (px)

DESCRIPTION

Here I discuss web service API best practices for django based projects

Citation preview

Page 1: API Design & Security in django

API Design & Security in Django

Tareque Hossain Education  Technology

1

Page 2: API Design & Security in django

2

Page 3: API Design & Security in django

Fundamentals of API

• Architecture

• Defining resources

• Uniform response

•  Serialization

• Versioning

• Authentication

3

Page 4: API Design & Security in django

Your API should be RESTful

•  Stateless

• Client-server

• Cacheable

• Uniform Interface o HTTP GET/POST/PUT/DELETE

4

Page 5: API Design & Security in django

Defining Resources

• Resource o Cohesive set of information

o Of interest to client

•  Identified by URL o Uniform Resource Locator

http://api.flickr.com/services/rest/?method=flickr.photos.getSizes&photo_id=5983860647

5

Page 6: API Design & Security in django

Defining Resources..

• Resource != Django Model o May consist of data from several different

model instances

• Attributes

• Values returned from member functions

o May contain data completely unrelated to any model instance

• Date & time of response

6

Page 7: API Design & Security in django

Resource: Example

7

Page 8: API Design & Security in django

Defining Resources...

• Notice how: o Each instance of book has (similar to

select_related):

• Authors

• Editions

• Awards

o is_favorite indicates whether the client user has marked this book as favorite

8

Page 9: API Design & Security in django

Uniform Response

9

Page 10: API Design & Security in django

Uniform Response

•  Resource attributes vary wildly

•  Provide uniform response: o Include resource independent attributes

• HTTP Status code • Error code (you define for your API)

• Error message or data

10

Page 11: API Design & Security in django

Uniformity: Example

http://api.pbslearningmedia.org/v1.0/likes/content/lsps07.sci.phys.matter

11

Page 12: API Design & Security in django

Uniform Response

•  Include meta information: o Facets for certain attributes

• Choices for form fields

o Pagination (if applicable) • Result count

• Page number

• Resource per page

12

Page 13: API Design & Security in django

Uniform Response

•  Present in all responses (GET/POST/PUT)

•  Not in response for DELETE

•  HTTP 1.1 forbids message body for 1.xx, 204 (DELETE) & 304

•  Can be parsed by client even if it can’t parse the actual resource data

13

Page 14: API Design & Security in django

Serialization

•  JSON rocks

•  RESTful API isn’t about restrictions

•  API should support: o JSONP

o JSON

o YAML

o XML

14

Page 15: API Design & Security in django

Serialization..

•  Have a default, say: JSON

•  But if client requests different format, then deliver accordingly (if supported)

http://api.pbslearningmedia.org/v1.0/content/contents/cdda1ed2-da03

http://api.pbslearningmedia.org/v1.0/content/contents/cdda1ed2-da03.xml

15

Page 16: API Design & Security in django

Serialization..

•  Have a default, say: JSON

•  But if client requests different format, then deliver accordingly (if supported)

http://api.pbslearningmedia.org/v1.0/content/contents/cdda1ed2-da03

http://api.pbslearningmedia.org/v1.0/content/contents/cdda1ed2-da03.xml

16

Page 17: API Design & Security in django

Versioning

•  APIs change all the time o Don’t break your existing API

o Roll out new API set while old ones are functioning (if data models don’t change)

•  Save namespace o Old

o New

http://api.pbslearningmedia.org/v1.0/content/contents/cdda1ed2-da03

http://api.pbslearningmedia.org/v2.0/content/contents/cdda1ed2-da03

17

Page 18: API Design & Security in django

Versioning •  Write separate URL definitions & handlers

for different versions

18

Page 19: API Design & Security in django

Authentication

19

Page 20: API Design & Security in django

Authentication

•  Not all APIs endpoints are public

•  Use authentication to protect your API o Oauth is great

http://wiki.oauth.net/w/page/12238551/ServiceProviders 20

Page 21: API Design & Security in django

Oauth: Overview

•  Two types of access: o Resource accessed by web applications

directly

• User independent

• Accessing Twitter’s aggregated public timeline

o Resource accessed by web applications on behalf of users

• Accessing user’s private timeline

21

Page 22: API Design & Security in django

Oauth: Overview •  Credentials consist of: o Consumer key & secret (application) o Access token & token secret (user)

•  Each request contains: o oauth_consumer_key o oauth_token o oauth_signature_method o oauth_signature o oauth_timestamp o oauth_nonce o oauth_version

22

Page 23: API Design & Security in django

Oauth: 2-legged

•  Resource accessed by web applications directly o Use 2-legged Oauth

o Leave oauth_token empty

http://oauth.googlecode.com/svn/spec/ext/consumer_request/1.0/drafts/2/spec.html

23

Page 24: API Design & Security in django

Oauth: 3-legged

•  Resource accessed by web applications on behalf of users o Use 3-legged Oauth

o User explicitly authorizes 3rd party applications to access protected resources

• Allow apps to fetch your tweet stream

http://www.flickr.com/services/api/auth.oauth.html

24

Page 25: API Design & Security in django

Oauth: Overview

25

Page 26: API Design & Security in django

Whoa..

•  Oauth can be overwhelming

•  But it’s great once you get to know it

•  API frameworks like django-piston supports Oauth out of the box

26

Page 27: API Design & Security in django

API Frameworks? •  API frameworks make it easier for you to

build APIs in django

•  Tastypie o  http://django-tastypie.readthedocs.org/en/latest/

•  django-piston o  https://bitbucket.org/jespern/django-piston/wiki/Home

•  django-rest-framework o  http://django-rest-framework.org/

•  dj-webmachine o  http://benoitc.github.com/dj-webmachine/

27

Page 28: API Design & Security in django

django-piston

•  At PBS Education, we chose django-piston o Primarily because of its built in Oauth support

•  Original release is not actively maintained

•  We have modified django-piston o To adapt the concepts I have discussed today

http://github.com/pbs-education/django-piston

28

Page 29: API Design & Security in django

Lets write some API •  Writing API using django-piston is easy

•  Instead of writing views for your URLs, write handlers

•  Extend piston’s BaseHandler class o Override following methods:

•  read for GET

• create for POST

• update for PUT

• delete for DELETE

29

Page 30: API Design & Security in django

30

Page 31: API Design & Security in django

31

Page 32: API Design & Security in django

urls.py

32

Page 33: API Design & Security in django

GET Response

33

Page 34: API Design & Security in django

POST Error Response

34

Page 35: API Design & Security in django

35

Page 36: API Design & Security in django

Q/A?

•  Slides are available at: o www.codexn.com

•  Presenting a talk on API at djangocon 2011

36

Page 37: API Design & Security in django

utils.py

37

Page 38: API Design & Security in django

auth.py

38