-
-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add new @login endpoint to return available external login options #1757
Open
erral
wants to merge
31
commits into
main
Choose a base branch
from
erral-login-options
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+226
−4
Open
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
cf79461
add new @login endpoint to return available external login options
erral 3ff293b
changelog
erral 783a079
lint
erral cc1347c
lint
erral 6cf2636
lint
erral 98c0cf5
lint
erral 68ce3be
lint
erral 46f891e
Merge branch 'main' into erral-login-options
erral 879752e
Update news/1757.feature
erral 53002d4
Update news/1757.feature
erral 37c47e7
Update news/1757.feature
erral 06be87c
add docs
erral 8168d4a
yaml
erral 961def7
yaml
erral 3cd4daa
docs
erral 0eccce4
docs
erral 499d74a
Review of docs
stevepiercy c97c97b
Revert `'` to `"`
stevepiercy 4cc4288
properly implement the adapter in tests
erral fd67f94
add docs rsults
erral 1a0c196
Merge branch 'main' into erral-login-options
erral 861673f
black
erral 7a9e378
fix response
erral 83c802e
Merge branch 'main' into erral-login-options
erral f52a73d
Merge branch 'main' into erral-login-options
erral 92ae900
Merge branch 'main' into erral-login-options
erral 49a018f
Merge branch 'main' into erral-login-options
erral b5c5899
Merge branch 'main' into erral-login-options
erral 730e8d2
Merge branch 'main' into erral-login-options
erral 3468580
Merge branch 'main' into erral-login-options
erral a57b3ac
rename the interface to ILoginProviders
erral File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
--- | ||
myst: | ||
html_meta: | ||
"description": "The @login endpoint exposes the list of external authentication services that may be used in the Plone site." | ||
"property=og:description": "The @login endpoint exposes the list of external authentication services that may be used in the Plone site." | ||
"property=og:title": "@login for external authentication links" | ||
"keywords": "Plone, plone.restapi, REST, API, login, authentication, external services" | ||
--- | ||
|
||
# Login for external authentication links | ||
|
||
It is common to use add-ons that allow logging in to your site using third party services. | ||
Such add-ons include using authentication services provided by KeyCloak, GitHub, or other OAuth2 or OpenID Connect enabled services. | ||
|
||
When you install one of these add-ons, it modifies the login process, directing the user to third party services. | ||
|
||
To expose the links provided by these add-ons, `plone.restapi` provides an adapter based service registration. | ||
It lets those add-ons know that the REST API can use those services to authenticate users. | ||
This will mostly be used by frontends that need to show the end user the links to those services. | ||
|
||
To achieve that, third party products need to register one or more adapters for the Plone site root object, providing the `plone.restapi.interfaces.IExternalLoginProviders` interface. | ||
|
||
In the adapter, the add-on needs to return the list of external links and some metadata, including the `id`, `title`, and name of the `plugin`. | ||
|
||
An example adapter would be the following, in a file named {file}`adapter.py`: | ||
|
||
```python | ||
from zope.component import adapter | ||
from zope.interface import implementer | ||
|
||
@adapter(IPloneSiteRoot) | ||
@implementer(IExternalLoginProviders) | ||
class MyExternalLinks: | ||
def __init__(self, context): | ||
self.context = context | ||
|
||
def get_providers(self): | ||
return [ | ||
{ | ||
"id": "myprovider", | ||
"title": "Provider", | ||
"plugin": "myprovider", | ||
"url": "https://some.example.com/login-url", | ||
}, | ||
{ | ||
"id": "github", | ||
"title": "GitHub", | ||
"plugin": "github", | ||
"url": "https://some.example.com/login-authomatic/github", | ||
}, | ||
] | ||
``` | ||
|
||
With the corresponding ZCML stanza, in the corresponding {file}`configure.zcml` file: | ||
|
||
```xml | ||
<adapter factory=".adapter.MyExternalLinks" name="my-external-links"/> | ||
``` | ||
|
||
The API request would be as follows: | ||
|
||
```{eval-rst} | ||
.. http:example:: curl httpie python-requests | ||
:request: ../../../src/plone/restapi/tests/http-examples/external_authentication_links.req | ||
``` | ||
|
||
The server will respond with a `Status 200` and the list of external providers: | ||
|
||
```{literalinclude} ../../../src/plone/restapi/tests/http-examples/external_authentication_links.resp | ||
:language: http | ||
``` |
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 @@ | ||
Add a `@login` endpoint to get external login services' links. @erral |
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,14 @@ | ||
# -*- coding: utf-8 -*- | ||
from plone.restapi.interfaces import ILoginProviders | ||
from plone.restapi.services import Service | ||
from zope.component import getAdapters | ||
|
||
|
||
class Login(Service): | ||
def reply(self): | ||
adapters = getAdapters((self.context,), ILoginProviders) | ||
external_providers = [] | ||
for name, adapter in adapters: | ||
external_providers.extend(adapter.get_providers()) | ||
|
||
return {"options": external_providers} |
3 changes: 3 additions & 0 deletions
3
src/plone/restapi/tests/http-examples/external_authentication_links.req
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,3 @@ | ||
GET /plone/@login HTTP/1.1 | ||
Accept: application/json | ||
Authorization: Basic YWRtaW46c2VjcmV0 |
19 changes: 19 additions & 0 deletions
19
src/plone/restapi/tests/http-examples/external_authentication_links.resp
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,19 @@ | ||
HTTP/1.1 200 OK | ||
Content-Type: application/json | ||
|
||
{ | ||
"options": [ | ||
{ | ||
"id": "myprovider", | ||
"plugin": "myprovider", | ||
"title": "Provider", | ||
"url": "https://some.example.com/login-url" | ||
}, | ||
{ | ||
"id": "github", | ||
"plugin": "github", | ||
"title": "GitHub", | ||
"url": "https://some.example.com/login-authomatic/github" | ||
} | ||
] | ||
} |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the difference between
id
andplugin
? Why do we need both?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is on purpose to support the case of a plugin added twice. Think of 2
oidc
plugins in a site. They would be different inid
but same inplugin
, like a way to mark the "plugin type" inplugin
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's what I do, for instance, in my PR on pas.plugins.oidc: https://github.com/collective/pas.plugins.oidc/blob/use-adapter/src/pas/plugins/oidc/services/login/get.py#L23-L26