We're sorry but this page doesn't work properly without JavaScript enabled. Please enable it to continue.
Feedback

How to make a full fledged REST API with Django OAuth Toolkit

00:00

Formale Metadaten

Titel
How to make a full fledged REST API with Django OAuth Toolkit
Serientitel
Teil
50
Anzahl der Teile
119
Autor
Lizenz
CC-Namensnennung 3.0 Unported:
Sie dürfen das Werk bzw. den Inhalt zu jedem legalen Zweck nutzen, verändern und in unveränderter oder veränderter Form vervielfältigen, verbreiten und öffentlich zugänglich machen, sofern Sie den Namen des Autors/Rechteinhabers in der von ihm festgelegten Weise nennen.
Identifikatoren
Herausgeber
Erscheinungsjahr
Sprache
ProduktionsortBerlin

Inhaltliche Metadaten

Fachgebiet
Genre
Abstract
synasius - How to make a full fledged REST API with Django OAuth Toolkit World is going mobile and the need of a backend talking with your apps is getting more and more important. What if I told you writing REST APIs in Python is so easy you don’t need to be a backend expert? Take generous tablespoons of Django, mix thoroughly with Django REST Framework and dust with Django OAuth Toolkit to bake the perfect API in minutes.
Schlagwörter
80
Vorschaubild
25:14
107
Vorschaubild
24:35
SynchronisierungEmulationREST <Informatik>SoftwareentwicklerComputeranimationVorlesung/Konferenz
REST <Informatik>MultiplikationsoperatorVorlesung/Konferenz
Weg <Topologie>Gleitendes MittelMultiplikationsoperatorDialektW3C-StandardInformationsmanagementDienst <Informatik>Framework <Informatik>Innerer PunktSpezielle unitäre GruppeEndliche ModelltheorieMehrwertnetzVarianzRechenwerkMetropolitan area networkCOMDatenmodellSichtenkonzeptAuthentifikationExogene VariableDateiformatMAPGesetz <Physik>SummierbarkeitURNCodeCodierungModelltheorieFormale SemantikSoftwaretestTaskArithmetisches MittelBildschirmmaskeIndexberechnungProjektive EbeneExogene VariableSerielle SchnittstelleInstantiierungDatenfeldPunktVererbungshierarchieKlasse <Mathematik>SchnittmengeAuthentifikationKartesische KoordinatenInteraktives FernsehenBrowserMailing-ListeFramework <Informatik>SichtenkonzeptDifferenteKontrast <Statistik>SelbstrepräsentationSystemplattformObjekt <Kategorie>Front-End <Software>MultiplikationsoperatorZweiDienst <Informatik>BenutzerbeteiligungVolumenvisualisierungWeb-ApplikationHumanoider RoboterREST <Informatik>DefaultTwitter <Softwareplattform>SoftwareentwicklerEinsParserDateiformatTextbausteinComputeranimation
QuantenzustandMetropolitan area networkMehrwertnetzLokales MinimumGleitendes MittelKlasse <Mathematik>SichtenkonzeptApp <Programm>ClientAutorisierungPasswortSCI <Informatik>Mailing-ListeGewicht <Ausgleichsrechnung>Framework <Informatik>CASE <Informatik>ServerKanal <Bildverarbeitung>CodeDatenflussAuthentifikationToken-RingCOMSoftware Development KitURNSoftwareentwicklerBildschirmmaskeCodeMustererkennungTopologieProgrammbibliothekIntegralCOMGeometrische QuantisierungResultanteStichprobenumfangTermDatenflussAbfrageServerExogene VariableGewicht <Ausgleichsrechnung>Serielle SchnittstelleCASE <Informatik>Anpassung <Mathematik>Coxeter-GruppeUltraviolett-PhotoelektronenspektroskopieFormation <Mathematik>PunktKlasse <Mathematik>Spezielle unitäre GruppeDatentransferKartesische KoordinatenInteraktives FernsehenBeobachtungsstudieBrowserFramework <Informatik>ClientWeg <Topologie>PasswortSchlüsselverwaltungAutorisierungMultiplikationsoperatorDatenreplikationSchlussregelDienst <Informatik>Office-PaketHumanoider RoboterDefaultBildschirmmaskeAuthentifikationService providerTextbausteinSichtenkonzeptGenerizitätFront-End <Software>URLSoftwareentwicklerApp <Programm>Computeranimation
Spezielle unitäre GruppeSoftwaretestAutorisierungCodeURNCAN-BusPunktKartesische KoordinatenAutorisierungSoftwareentwicklerSichtenkonzeptProgramm/QuellcodeXMLJSON
URNNetzwerkbetriebssystemWurzel <Mathematik>RechenwerkBrennen <Datenverarbeitung>Neuronales NetzRuhmasseInnerer PunktCAN-BusKartesische KoordinatenClientFramework <Informatik>Programm/QuellcodeXML
URNWurzel <Mathematik>DreiNetzwerkbetriebssystemSystemaufrufZeitbereichMetropolitan area networkMenütechnikCAN-BusTotal <Mathematik>CodeBildschirmmaskeIndexberechnungE-MailExogene VariableKartesische KoordinatenMailing-ListeClientAutorisierungAuthentifikationURLProgramm/Quellcode
SoftwaretestAutorisierungCodeNormierter RaumOffene MengeInformationsspeicherungHilfesystemBenutzerschnittstellenverwaltungssystemOffene MengeInformationsspeicherungKartesische KoordinatenHilfesystemMultiplikationsoperatorOffice-PaketAutomatische HandlungsplanungComputersicherheitComputeranimation
ImplementierungMereologieServerFramework <Informatik>ClientAutorisierungTwitter <Softwareplattform>UmwandlungsenthalpieVorlesung/Konferenz
CodeModelltheorieMAPGruppenoperationMereologieServerSerielle SchnittstelleCoxeter-GruppeKlasse <Mathematik>AutorisierungObjekt <Kategorie>Rechter WinkelDienst <Informatik>VererbungshierarchieKomponente <Software>Vorlesung/Konferenz
AbfrageInstantiierungFilter <Informatik>Vorlesung/Konferenz
Vorlesung/Konferenz
Transkript: Englisch(automatisch erzeugt)
Okay, ready for the next talk, then? This is Federico who is a C++ and Python developer at Evernote, and he's going to
talk about how to make a full-fledged REST API with Django and OAuth toolkit. Over to you.
Hi everyone, so the goal of this talk is to show you how to create a REST API protected with OAuth tool, but first I want to tell you how you should know how to
tell you a story. So let me introduce you to this small and simple application, the time tracker, which
is of course a web application that tracks, allows the users to track the time they spend on their activities. And, at the beginning, at first, I had to choose to pick one tool, which was Django,
and I had one single big project, and I deployed it once, and everything was fine, more or less, but as he used to sing, the times, they are changing, and what has changed?
Actually, well, front-end development has changed a lot, sorry, today we have a lot of web front-end frameworks, and that allows you to create amazing front-end applications,
and they have their own development tools, to build, to test, and to run the application, and so they are completely separate applications, and I also had to support multiple devices,
which means to support different browsers, and different platforms, but I should also take care of the native applications, so I ended up with a lot of projects, I had
time tracker backend, time tracker web, which is the web front-end application, and the android project, the IIS project, and the desktop application, you know, for the old
desktops, and moreover, there are third-party services that wants to connect with my time
tracker application, they want to send me data, they want to read data from my time tracker applications, so what happens in the backend application, what's in the backend application, there is a service that exposes an amazing and reliable REST API, and this
is the recipe, Django, Django REST framework, and Django OAuth toolkit, these are the models, they are really really simple, there is the activity and the time entry, which is the model
that allows us to track the user, the time that a user spends on an activity or a task, and these are the endpoints that I want to create, on the most left column,
the URL, the HTTP method supported, and the semantic meaning of each method, so, for example, the first row, slash API slash activities, if you send a GET request, you get back
a list of the available activities. To create the endpoint, I need you to, I need you, I need
five minutes, so the first thing you have to do is to serialize your data, and this is really really straightforward in Django REST framework, you can use this base class, serializer, and this works just like Django forms, so you just define the fields you
need, and add some code to restore or create the instance of the model from its serialized representation, and then you can use, easily use the serializer, and you'll get back a dictionary representation of your object.
Of course, this is boilerplate code, which should be repeated for every model of your application, but you can avoid to write that code using model serializer, which allows you
to just specify which is the model you are serializing, you want to serialize. Then we have to create the views, the endpoints, and what do we need? We need, of course, to respect the semantic meaning, but we should take care of the user
authentication, and also we should take care of permission checks, also sometimes object-level permissions, sometimes you need to paginate your endpoints because you get a lot of results,
and also you want maybe to handle response and request formatting to support, for example, JSON, XML, YAML, so this is a lot of stuff, but you just keep calm and use Django REST
framework, because Django's framework is really, has a lot of settings that allows you to customize its behavior, its default behavior, these are just a small example, the first one allows you to define which is the class that takes care of the user authentication, and then we have the
default permission class, so if you're not authenticated, you won't get anything, just 403, and the default renderer and default parser for the formatting,
for the format. Well, to create the endpoint, you can use the API view based class provided by Django REST framework, which allows you to add some code to the handler meters,
and this class will use, this base class will use the settings, we can see here, to realize, to create an endpoint with the correct behavior, with the behavior you want, and well, the code, it is really, really easy to understand,
here we have the query set to retrieve all the activities, we serialize the query set, and return the serialized response, and of course, this is boilerplate code, so you can
you have to repeat this code, 44 endpoints, but you can avoid to write this code using the generic class-based view provided by Django REST framework,
here you just need to specify which is the base query set and the serializer class you want to use, there is also a built-in browsable API provided by Django REST framework,
which we are going to see at the end of the talk, so the next step is how do you authorize client applications, I mean, your applications like the time tracker
Android and the time tracker iOS, they need to be authorized to talk to the time tracker backend API, and also there are third-party apps that want to access your users' data, so you need an authorization, you need some authorization engine behind,
if you don't have this authorization engine, these are the problems you're going to face, first of all, you have to store, the first solution without the
authorization framework is to store the user password in the application, which is not good, of course, because the application gets a full access to the user account, and if the user wants to revoke the access to
the application, he needs to change his password, so also compromised apps can expose the user password and username, this is the solution,
the auth2 authorization framework, so how does it work, and I want to explain how it works using this simple use case, so imagine that there is a songify
music streaming service that wants to connect with the time tracker application, so their users can track their listening activities on the time tracker application,
these are the actors, this terminology is the same used in the RFC,
in the auth2 RFC, and I'm just trying to translate these terms to this use case, so the results owner is of course the user, the resource server is the time tracker API, and the authorization server in this case is the same as the results server, and the client
is the songify application, I want to explain you what the auth2 authorization framework defines four flows, four authorization flows, I want to show you how one of these flows work,
this is the most popular one, the authorization code flow, so the first step is when the client registers with the authorization server, and the authorization server provides a client ID and a client secret, the client of course is the songify application, so there is someone
at songify that for example goes to developer.timetracker.com, add a developer application, and it gets back a client ID and a client secret key, the second step
is when the songify application redirects the user to the time tracker application via its user agent, via its browser for instance, and next the time tracker application authenticates
the user and obtains the authorization to communicate with the songify application
from the user, now the time tracker application redirects the user back to the songify application with an authorization code, which is later exchanged for a token, and the token
can be used by the client to authenticate requests, how to do that in Django? With Django our toolkit of course, which supports Django from 1.4 to 1.7, Python 2 and Python 3, and it is built on top of altlib, which actually is a really great library,
it takes care of the compliance with the RFC, we just wrote some glue code, and its integration with Django is really really easy, you add the alt2 provider application
to the installed apps, add our URLs to your patterns, and you can create a protected endpoint using our generic protected resource view, and here you have an API,
an endpoint which is protected with alt2, now it comes with batteries included, so we have built-in views to register developer apps and a form view for the user for the user
authorization, it is integrated with Django rest framework, you just need to switch the default authentication classes with the one provided by Django toolkit package, and now I want to show you how it works, so these are the steps,
we are going to the authorization, first we are going to create a developer application, then we are going to simulate the step when the user is redirected to the authorization
endpoint, so here you can see this is one of the built-in views to register new developer applications, so you create new application, you add the name, here you have your client ID
and client secret, you can choose these are details from the alt2 framework, anyway I got my songify application ready, here it is, so
we can just use this one to step one, the songify application is redirecting the user agent
of the user to the authorization form, but first the user has to authenticate, and now the application is asking for my authorization, and we authorize, of course,
come on, okay, of course this should be the URL of the client application, so songify.com
for example, now we can take this authorization code, just substitute the code here, and we are going to exchange the code to obtain a token, here is the response, and
the token can be used to create a proper request with this header authorization bearer
with the token, so just want to show you that I'm not lying, if I try to get the list of the activities, just tell me that I'm missing the authentications credential, if I use
my new token, I can get back the list of the activities, so, and that's all, the future plans for Django toolkit are to support alt1, maybe add support for the open ID connector,
RFC, which I really don't know, I still have to read the paper, and add no secure storage, storage support for the application's storage, so we need some help, and thank you, that's all.
Any questions, please, anyone? First, thank you for your talk, my question is,
can we use the same framework to post tweets on Twitter? No, Django toolkit actually is the
implementation of the server side part of the outer specification, so if you want to send tweets to Twitter, you need a client side implementation of the alt2
RFC, the authorization framework. In your examples, the authorization server was always the same as the actual service, with your toolkit, can you make two services,
can you separate out the authorization part? Actually, we have to work to keep the resource server and the authorization server separated, yes, but maybe
you can, actually you have to write some more code to keep the authorization server and the so if you want to expose your API differently, your resources differently from the actual model
definition, can you simply handle that in the serializer class so you don't have to use the Django and the serializer? Yes, of course, you just use serializer, if your data is, for example, MongoDB, you just use the serializer base class
to write your own serializer and it just works, you have to write some more code, but it works, it is okay. Okay, thanks. You're welcome. Hello, thanks for your talk,
what are you using for object ownership? Sorry? Object ownership, so how do you say this object belongs to this user and I only showed this request coming from this user? Oh,
well, this is object level permission. But you are using any component for that? We are just filtering the query set, looking at the, well, actually, okay,
sometimes we can get back which is the user bound to the token, okay, and with the user instance you can filter the query set. This is a solution, really simple solution. Any more questions?
Okay, thank you very much.