You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/technologies/flask/oauth-google.mdx
+247-1Lines changed: 247 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,4 +10,250 @@ import SubHeading from "@site/src/components/SubHeading";
10
10
11
11
> Related Sample: https://github.com/app-generator/flask-google-oauth
12
12
13
-
This content is work in progress (soon available)
13
+
## Introduction to OAuth
14
+
15
+
OAuth which stands for Open Authorization is a protocol that allows users to grant third-party applications access to their data without giving away their passwords. OAuth works by giving each user a unique identifier and secret, which they can use to authorize applications to access their data.
16
+
17
+
OAuth is a secure protocol that protects user data. The user's password is never shared with the website or app. The OAuth authorization server only grants access to the user's data if the user explicitly grants permission.
18
+
19
+
OAuth is a widely used protocol. It is supported by many popular websites and apps, including Google, Facebook, Twitter, and Amazon. OAuth is a secure and convenient way for users to share their data with third-party applications.
20
+
21
+
Here are some of the benefits of using OAuth:
22
+
23
+
- Security: OAuth protects user data by never sharing the user's password with the third-party application.
24
+
- Convenience: OAuth makes it easy for users to share their data with third-party applications.
25
+
- Flexibility: OAuth is a flexible protocol that can be used to access a wide variety of data.
26
+
- Interoperability: OAuth is a widely supported protocol that can be used with a wide variety of third-party applications.
27
+
28
+
If you are developing a website or app that needs to access user data, OAuth is a good option to consider. OAuth is a secure, convenient, and flexible protocol that can help you to protect user data and make it easy for users to share their data with your application.
29
+
30
+
## Creating a Google OAuth Client ID
31
+
32
+
To create a Google OAuth Client ID:
33
+
- You will need to create a Google Cloud Platform project. Open Google Cloud Platform [here](https://console.cloud.google.com/)
34
+
- Once you have created a project, you can go to the API Credentials page and click the "Create Credentials" button.
35
+
- Select "OAuth Client ID" and then "Web application".
36
+
- Enter a name for your application and click the "Create" button. You will then be given a Client ID and Client Secret.
37
+
38
+
## Setting up Flask application
39
+
Flask is a microframework for Python web development. It is a simple, flexible, and extensible framework that can be used to create a wide variety of web applications.
40
+
41
+
This tutorial will focus on setting up OAuth for an existing Flask template. The template is hosted on Github and will be modified to accept user authorization using OAuth.
42
+
43
+
- Open the terminal and clone the repository using the command
Flask-Dance is a Flask extension that makes it easy to add OAuth authentication to your Flask apps. Flask-Dance provides a single, consistent API for working with OAuth providers, making it easy to add authentication to your app without having to learn the specific OAuth protocol for each provider.
98
+
99
+
Flask-Dance supports a wide variety of OAuth providers, including Google, Facebook, Twitter, GitHub, Bitbucket, Instagram, Reddit, Spotify, and many more
100
+
101
+
- Install `flask-dance` by executing the command on your terminal
Some changes will be made to the project files to add an authentication mechanism using Google OAuth. Below is the current folder structure of the project.
108
+
```bash
109
+
.
110
+
├── apps
111
+
│ ├── authentication
112
+
│ ├── config.py
113
+
│ ├── home
114
+
│ ├── __init__.py
115
+
│ ├── static
116
+
│ └── templates
117
+
├── package.json
118
+
├── README.md
119
+
├── render.yaml
120
+
├── requirements.txt
121
+
└── run.py
122
+
```
123
+
- We will be creating an authentication schema that will be used to save authentication tokens when a user login using Google authentication. Make the following changes to `apps/authentication/models.py`
124
+
125
+
```py
126
+
# apps/authentication/models.py
127
+
128
+
from flask_login import UserMixin
129
+
from flask_dance.consumer.storage.sqla import OAuthConsumerMixin
130
+
131
+
from apps import db, login_manager
132
+
133
+
from apps.authentication.util import hash_pass
134
+
135
+
classUsers(db.Model, UserMixin):
136
+
137
+
__tablename__ ='users'
138
+
139
+
id= db.Column(db.Integer, primary_key=True)
140
+
username = db.Column(db.String(64), unique=True)
141
+
email = db.Column(db.String(64), unique=True)
142
+
password = db.Column(db.LargeBinary)
143
+
oauth_google = db.Column(db.String(100), nullable=True) # <-- NEW attribute
144
+
...
145
+
146
+
classOAuth(OAuthConsumerMixin, db.Model): # <-- NEW class
- We will be creating an `oauth` module to handle the signup and login using Google authentication. Create a file `oauth.py` in the `apps/authentication` folder and add the following code
152
+
```py
153
+
# apps/authentication/oauth.py
154
+
import os
155
+
156
+
from flask_login import current_user, login_user
157
+
from flask_dance.consumer import oauth_authorized
158
+
from flask_dance.consumer.storage.sqla import SQLAlchemyStorage
159
+
from flask_dance.contrib.google import google, make_google_blueprint
We have created a blueprint that makes use of Google's authentication. We connected the blueprint to the `OAuth` schema.
201
+
202
+
- Next step is to register the blueprint to our Flask application to make it accessible from the browser route. Update `apps/__init__.py` to register the blueprint
203
+
```py
204
+
# apps/__init__.py
205
+
...
206
+
defregister_blueprints(app):
207
+
from apps.authentication.oauth import google_blueprint
- Next step is creating a route for the google blueprint that will handle requests for the authentication. Update `apps/authentication/routes.py` with the following code
225
+
```py
226
+
# apps/authentication/routes.py
227
+
...
228
+
from flask_dance.contrib.google import google
229
+
from apps.authentication import blueprint
230
+
231
+
@blueprint.route("/google")
232
+
deflogin_google():
233
+
""" Google login """
234
+
ifnot google.authorized:
235
+
return redirect(url_for("google.login"))
236
+
237
+
res = google.get("/user")
238
+
return redirect(url_for('home_blueprint.index'))
239
+
```
240
+
Now requests to `login/google` route will be handled by this route. From your terminal you can run the command `flask routes` to see all the routes present in the application.
241
+
242
+
- Start up the application from the terminal using the command
243
+
```bash
244
+
(venv) flask-google-oauth$ flask run
245
+
```
246
+
From your browser visit [`127.0.0.1:5000/login/google`](http://127.0.0.1:5000/login/google). Once you have logged in, you will be able to access protected resources in your Flask app.
247
+
248
+
## Conclusion
249
+
In conclusion, this article has provided an overview of OAuth and its importance in modern web applications. We explored the process of creating a Google OAuth Client ID, which is essential for enabling user authentication and authorization using Google accounts. Additionally, we discussed the steps involved in setting up a Flask application, a popular Python web framework, to handle OAuth authentication.
250
+
251
+
To facilitate OAuth functionality in our Flask application, we installed and configured Flask-Dance, a Flask extension that simplifies the integration of OAuth providers. Specifically, we focused on configuring Flask-Dance with Google OAuth, allowing users to authenticate with their Google accounts in our application.
252
+
253
+
By following the steps outlined in this article, developers can leverage the power of OAuth and Google OAuth specifically to enable secure user authentication and authorization in their Flask applications. This process not only enhances the user experience but also provides a seamless integration with widely used social media and identity platforms.
0 commit comments