-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fake-oauth server Simple server faking google oauth. Changes in backend-project to be implemented in the next commit. * FakeProvider talking to fake-oauth * Configure oauth provider with a flag. Added a `SOCIAL_AUTH_USE_FAKE_OAUTH` flag that, if set to true, will configure the backend to use a fake oauth server. Talks to a real google oauth api otherwise. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Lint fixes * Mention fake-oauth in the main README. * Disable fake_oauth for tests. * Mention issues with old sessions in README Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
1cd57b7
commit ad94772
Showing
9 changed files
with
131 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
from django.conf import settings | ||
from django.core.exceptions import ImproperlyConfigured | ||
from requests_oauthlib import OAuth2Session | ||
|
||
|
||
|
@@ -31,3 +33,41 @@ def exchange(self, request): | |
) | ||
resp = google.get(self.userinfo_url) | ||
return resp.json() | ||
|
||
|
||
class FakeProvider: | ||
base_url = "https://localhost:5678" | ||
|
||
def __init__(self, *args, **kwargs): | ||
pass | ||
|
||
def callback_url(self, request): | ||
# Hardcode localhost - the provider is expected to be used only in local | ||
# deployments. | ||
# `build_absolute_uri` doesn't work, because it produces a docker | ||
# friendly url. | ||
redirect_uri = "http://localhost:8000/login/callback" | ||
return f"{self.base_url}?redirect_uri={redirect_uri}", None | ||
|
||
def exchange(self, request): | ||
# Hardcoded values. | ||
# Simple, but working. | ||
return { | ||
"email": "[email protected]", | ||
"given_name": "GivenName", | ||
"family_name": "FamilyName", | ||
} | ||
|
||
|
||
def get_provider_cls(): | ||
flag_value = settings.SOCIAL_AUTH_USE_FAKE_OAUTH | ||
if flag_value is True: | ||
if not settings.DEBUG: | ||
raise ImproperlyConfigured("Fake oauth may only be used in DEBUG mode") | ||
return FakeProvider | ||
elif flag_value is False: | ||
return GoogleProvider | ||
else: | ||
raise ImproperlyConfigured( | ||
f"Fake oauth must be either True or False, is {flag_value}" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
FROM node:latest | ||
|
||
WORKDIR oauth | ||
|
||
# Generate a self signed certificate. This will let us handle https requests. | ||
RUN openssl genrsa -out key.pem | ||
RUN openssl req -new -key key.pem -out csr.pem -batch | ||
RUN openssl x509 -req -days 9999 -in csr.pem -signkey key.pem -out cert.pem | ||
RUN rm csr.pem | ||
|
||
COPY server.js ./ | ||
|
||
EXPOSE 5678 | ||
|
||
CMD ["node", "server.js"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# small_eod fake-oauth | ||
|
||
Zawiera prosty serwer umożliwiający lokalne testowanie aplikacji, bez konieczności komunikacji z serwerem oauth. | ||
|
||
Tylko i wyłącznie do lokalnego testowania. | ||
|
||
## Uruchamianie | ||
|
||
Serwer uruchomi się razem z pozostałymi komponentami przy użyciu docker-compose. | ||
Uwaga - aby aplikacja korzystała z fake-oauth zamiast prawdziwego serwera oauth, konieczne są zmiany w konfiguracji backend-project. Więcej informacji można znaleźć w folderze projektu backendowego. | ||
Zaimplementowano jedynie absolutne minimum funkcjonalności. | ||
|
||
## Ostrzeżenie przeglądarki | ||
|
||
Większość przeglądarek wyświetli ostrzeżenie przy pierwszym kontakcie z serwerem, spododowane samodzielnie podpisanym certyfikatem. | ||
O ile w przypadku publicznych serwerów jest to oznaka potencjalnego zagrożenia, w tym przypadku ostrzeżenie można zignorować. | ||
|
||
## Pętla przekierowań | ||
|
||
Jeśli przeglądarka ma zapisane w pamięci cookie po zalogowaniu do serwisu (powstałe, na przykład, po zalogowaniu poprzed panel admin/), próba logowania | ||
może się nie powieść. Rozwiązaniem jest usunięcie cookie lub użycie karty prywatnej do testowania. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* A *very* simple server, redirecting all requests to a specified url. | ||
*/ | ||
const https = require("https"); | ||
const fs = require("fs"); | ||
const url = require("url"); | ||
|
||
const PORT = 5678; | ||
|
||
// Read generated self-signed certificate to serve https. | ||
// See the Dockerfile for details. | ||
const options = { | ||
key: fs.readFileSync("key.pem"), | ||
cert: fs.readFileSync("cert.pem"), | ||
}; | ||
|
||
console.log(`Starting a server at :${PORT}`); | ||
|
||
https | ||
.createServer(options, function (req, res) { | ||
const q = url.parse(req.url, true).query; | ||
const { redirect_uri } = q; | ||
|
||
if (!redirect_uri) { | ||
throw new Error("redirect_uri must be specified"); | ||
} | ||
|
||
// The content doesn't matter - it's hardcoded in the server. | ||
// The only important bit is the Location header - the backend should | ||
// provide a url it would normally expect the oauth server to redirect to. | ||
// NOTE: the url must be absolute, not docker friendly, i.e. it should | ||
// start with "localhost", not "backend-project". | ||
res.writeHead(302, { Location: redirect_uri }); | ||
res.end("Fake oauth reply"); | ||
}) | ||
.listen(PORT); |