Skip to content
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

API: Add support for OAuth2 Client Credentials and Access Tokens #3943

Closed
3 tasks done
lastzero opened this issue Dec 9, 2023 · 14 comments
Closed
3 tasks done

API: Add support for OAuth2 Client Credentials and Access Tokens #3943

lastzero opened this issue Dec 9, 2023 · 14 comments
Assignees
Labels
authentication User Account Management and Authentication enhancement Optimization, improvement or maintenance task security Impact on server or browser security tested Changes have been tested successfully

Comments

@lastzero
Copy link
Member

lastzero commented Dec 9, 2023

External applications must be able to authenticate with OAuth2 Client Credentials in order to obtain valid Access Tokens for communication with our REST API.

Further OAuth2 use cases and authentication options are beyond the scope of this issue. They may be added over time after this has been implemented.

Acceptance Criteria:

  • In addition to (a) implementing a POST /api/v1/oauth/token endpoint for creating access tokens, this includes (b) adding support for standard Bearer Token authentication headers and (c) a minimum of scope-based authorization checks.
  • As a first step, Prometheus should be able to query the GET /api/v1/metrics endpoint with authentication so that it won't need to be publicly accessible: API: Expose Prometheus-style metrics endpoint #3730
  • Helpful implementation details and usage examples should be added to the docs, so developers understand the authentication options and know how to use the API: https://docs.photoprism.app/developer-guide/

Related Issues:

Protocol References:

Authentication Libraries:

Documentation Examples:

@lastzero lastzero added enhancement Optimization, improvement or maintenance task security Impact on server or browser security labels Dec 9, 2023
@lastzero lastzero self-assigned this Dec 9, 2023
@lastzero lastzero added the in-progress Somebody is working on this label Dec 9, 2023
lastzero added a commit that referenced this issue Dec 9, 2023
Signed-off-by: Michael Mayer <michael@photoprism.app>
@lastzero
Copy link
Member Author

I will push my changes and provide an updated preview build for testing as soon as the new CLI commands (almost done) and the scope-based authorization checks are implemented, so probably early next week :)

lastzero added a commit that referenced this issue Dec 12, 2023


This adds standard OAuth2 client credentials and bearer token support as
well as scope-based authorization checks for REST API clients. Note that
this initial implementation should not be used in production and that
the access token limit has not been implemented yet.

Signed-off-by: Michael Mayer <michael@photoprism.app>
@lastzero
Copy link
Member Author

My last commit adds standard OAuth2 client credentials and bearer token support as well as scope-based authorization checks for REST API clients:

  • Note that this first implementation should not be used in production and that the optional access token limit has not been implemented yet.
  • If you would like to test these changes without building from source, you can use the photoprism/photoprism:test image available on Docker Hub: https://hub.docker.com/r/photoprism/photoprism/tags?page=1&name=test
  • Use the photoprism clients CLI subcommands to list, create, update and delete clients and then test the credentials e.g. with the new GET /api/v1/metrics endpoint. The required scope for this is "metrics", but the "*" setting should also work.

@lastzero lastzero added the please-test Ready for acceptance test label Dec 12, 2023
@lastzero
Copy link
Member Author

lastzero commented Dec 13, 2023

Here's what I think is still needed to make this releasable:

  • The ability to use client authentication with a bearer token header for WebDAV access too
  • Enforcement of the Access Token limit, so that clients cannot create as many tokens as they like
  • Add /.well-known/ endpoints for automatic client configuration
  • Helpful documentation with implementation details and usage examples
  • A little more time so it can be tested in a variety of scenarios, e.g. together with 2FA

@brandon1024
Copy link
Sponsor Contributor

I'll happily take on documentation tasks related to the setup of prometheus :-)

@lastzero
Copy link
Member Author

lastzero commented Dec 14, 2023

If you would like to test the new POST /api/v1/oauth/token endpoint, you can use the photoprism/photoprism:test image available on Docker Hub and, for example, run this in a terminal to create a new access token:

curl -Ss -X POST http://localhost:2342/api/v1/oauth/token \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'grant_type=client_credentials&scope=metrics&client_id=ID&client_secret=SECRET'

Simply replace ID and SECRET with the actual client ID and secret created using the new photoprism clients add command and make sure the base URL is correct, i.e. you may also need to change http://localhost:2342/.

graciousgrey added a commit that referenced this issue Dec 14, 2023
graciousgrey added a commit that referenced this issue Dec 15, 2023
graciousgrey added a commit that referenced this issue Dec 21, 2023
graciousgrey added a commit that referenced this issue Dec 21, 2023
lastzero added a commit that referenced this issue Jan 2, 2024
Signed-off-by: Michael Mayer <michael@photoprism.app>
lastzero added a commit that referenced this issue Jan 5, 2024
You can now run "photoprism auth add" to create new client access tokens
that allow external applications to use the built-in REST API.

Signed-off-by: Michael Mayer <michael@photoprism.app>
lastzero added a commit that referenced this issue Jan 6, 2024
Signed-off-by: Michael Mayer <michael@photoprism.app>
@lastzero
Copy link
Member Author

lastzero commented Jan 6, 2024

With the commits referenced above, you can now create personal access tokens for authentication with our API without having to first register an OAuth2 client application, e.g. by running the following command in a terminal:

photoprism auth add --name MyApp --user admin

To see all supported command flags:

photoprism auth add --help

Note that there is no updated development preview for testing yet. So if you want to try this, you will need to build from source until we feel it has been tested enough to make it available to everyone.

lastzero added a commit that referenced this issue Jan 7, 2024


These changes ensure that the new (SHA256) session ID is returned in the
"session_id" field, so that developers have time to update their client
implementations to use the new "access_token" field.

Signed-off-by: Michael Mayer <michael@photoprism.app>
@lastzero
Copy link
Member Author

lastzero commented Jan 7, 2024

@Radiokot I've started working on a /.well-known/oauth-authorization-server service discovery endpoint for OAuth2-compatible API clients:

well-known

To complete this, it would be good to know which fields you actually need?

We currently don't use JWT tokens, so the related fields don't seem to be necessary and could be left blank or omitted depending on what is most compatible/easiest for API clients to handle?

Please also let us know if there are any other service discovery endpoints we should add :)

@Radiokot
Copy link
Collaborator

Radiokot commented Jan 7, 2024

@lastzero in order to initialize the mobile OpenID connector, only the following fields are required:

  • authorization_endpoint
  • token_endpoint

@lastzero
Copy link
Member Author

lastzero commented Jan 8, 2024

@Radiokot To authenticate with the API, you can currently create an access_token (aka "auth token") with the photoprism auth add command in a terminal (later also through the user interface). In this case, the information under /.well-known/oauth-authorization-server does not seem to be needed at all?

Alternatively, you can use the photoprism client add command to create client credentials (client_id and client_secret) to get access tokens from the token_endpoint. For this, the information about the token endpoint URL and the available scopes seems to be somewhat useful and should help to reduce manual configuration?

An authorization_endpoint does not exist yet. However, if it is helpful to e.g. connect your mobile app in a (more convenient) way, I can look into that and provide an implementation for that specific use case. Could you provide me with more specifics on this, e.g. what flow and credentials are being used/expected?

@Radiokot
Copy link
Collaborator

Radiokot commented Jan 8, 2024

@lastzero if, as we discussed earlier, if the user will be able to create a perpetual access token for a third-party app in the web interface, then it makes sense to add auth by token as an alternative to username+password, and in this case the app has nothing to do with /.well-known.

If PhotoPrism itself is going to be an OAuth/OpenID server, then the apps will be able to get access tokens through the Authorization Code Flow, allowing the user to sign in and grant the access on a web page. In this case, .well-known/openid-configuration should return both authorization_endpoint and token_endpoint so the app could 1) render the auth page and get the auth code; 2) exchange the auth code for the access token.

I don't think, however, that you should spend time implementing the authorization endpoint, at least for the first release. For me, having the ability to create an access token in the web app and then paste it to the 3rd party app is enough.

lastzero added a commit that referenced this issue Jan 8, 2024


This commit also adds an /api/v1/oauth/logout endpoint that allows
clients to delete their sessions (access tokens) as needed.

Signed-off-by: Michael Mayer <michael@photoprism.app>
@lastzero
Copy link
Member Author

lastzero commented Jan 8, 2024

@Radiokot A simple /.well-known/oauth-authorization-server endpoint is now implemented and can be tested with the updated photoprism/photoprism:test image available on Docker Hub:

{
  "issuer": "http://localhost:2342/",
  "authorization_endpoint": "",
  "token_endpoint": "http://localhost:2342/api/v1/oauth/token",
  "registration_endpoint": "",
  "response_types_supported": [
    "token"
  ],
  "response_modes_supported": [],
  "grant_types_supported": [
    "client_credentials"
  ],
  "subject_types_supported": [],
  "scopes_supported": [
    "shares",
    "videos",
    "places",
    "feedback",
    "folders",
    "favorites",
    "albums",
    "moments",
    "logs",
    "metrics",
    "photos",
    "calendar",
    "people",
    "settings",
    "services",
    "users",
    "files",
    "labels",
    "config",
    "password",
    "webdav"
  ],
  "token_endpoint_auth_methods_supported": [
    "client_secret_basic",
    "client_secret_post"
  ],
  "claims_supported": [],
  "code_challenge_methods_supported": [],
  "introspection_endpoint": "",
  "introspection_endpoint_auth_methods_supported": [],
  "revocation_endpoint": "http://localhost:2342/api/v1/oauth/revoke",
  "revocation_endpoint_auth_methods_supported": [
    "none"
  ],
  "end_session_endpoint": "",
  "request_parameter_supported": false,
  "request_object_signing_alg_values_supported": [],
  "device_authorization_endpoint": "",
  "dpop_signing_alg_values_supported": []
}

While implementing this, I also added a POST /api/v1/oauth/revoke endpoint so that API clients can delete their sessions (access tokens) again when they are not needed anymore.

lastzero added a commit that referenced this issue Mar 21, 2024


Signed-off-by: Michael Mayer <michael@photoprism.app>
@lastzero lastzero added authentication User Account Management and Authentication and removed in-progress Somebody is working on this labels Mar 22, 2024
graciousgrey added a commit that referenced this issue Mar 22, 2024
lastzero added a commit that referenced this issue Mar 26, 2024
This is to prevent prometheus and keycloak from starting automatically
and allows developers to run them only when needed, see:
https://docs.docker.com/compose/profiles/

Signed-off-by: Michael Mayer <michael@photoprism.app>
lastzero added a commit that referenced this issue Mar 26, 2024
Signed-off-by: Michael Mayer <michael@photoprism.app>
graciousgrey added a commit that referenced this issue Mar 26, 2024
@lastzero lastzero changed the title OAuth2: Support Client Authentication API: Add support for OAuth2 client credentials and access tokens Mar 27, 2024
@lastzero lastzero changed the title API: Add support for OAuth2 client credentials and access tokens OAuth2: Add support for authentication with client credentials and access tokens Mar 27, 2024
@lastzero lastzero changed the title OAuth2: Add support for authentication with client credentials and access tokens API: Add support for OAuth2 Client Credentials and Access Tokens Mar 27, 2024
graciousgrey added a commit that referenced this issue Mar 27, 2024
graciousgrey added a commit that referenced this issue Mar 27, 2024
lastzero added a commit that referenced this issue Apr 4, 2024
Signed-off-by: Michael Mayer <michael@photoprism.app>
lastzero added a commit that referenced this issue Apr 10, 2024
Signed-off-by: Michael Mayer <michael@photoprism.app>
lastzero added a commit that referenced this issue Apr 12, 2024
Signed-off-by: Michael Mayer <michael@photoprism.app>
@lastzero
Copy link
Member Author

An updated preview build is now available on Docker Hub for final testing:

Any help with that is much appreciated! 🎉

lastzero added a commit that referenced this issue Apr 17, 2024


Signed-off-by: Michael Mayer <michael@photoprism.app>
lastzero added a commit that referenced this issue Apr 17, 2024
Signed-off-by: Michael Mayer <michael@photoprism.app>
lastzero added a commit that referenced this issue Apr 17, 2024
Signed-off-by: Michael Mayer <michael@photoprism.app>
@graciousgrey graciousgrey added tested Changes have been tested successfully and removed please-test Ready for acceptance test labels Apr 18, 2024
lastzero added a commit to photoprism/photoprism-docs that referenced this issue Apr 23, 2024
lastzero added a commit to photoprism/photoprism-docs that referenced this issue Apr 23, 2024
@lastzero
Copy link
Member Author

I have added the CLI command documentation to the User Guide, so that it's easy to find:

Any contributions that help improve the API documentation and make it easier to use for developers would be much appreciated:

We also plan to work on that, but we don't know when we will get to it due to the many other things on our plate.

lastzero added a commit to photoprism/photoprism-docs that referenced this issue Apr 23, 2024
lastzero added a commit to photoprism/photoprism-docs that referenced this issue Apr 24, 2024
@brandon1024
Copy link
Sponsor Contributor

@lastzero The documentation was very clear! Upgraded photoprism and hooked it into my prometheus instance without a hitch!

Thanks for your amazing efforts 🙌 I'll buy you a beer next time I'm in Berlin!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
authentication User Account Management and Authentication enhancement Optimization, improvement or maintenance task security Impact on server or browser security tested Changes have been tested successfully
Projects
Status: Release 🌈
Development

No branches or pull requests

4 participants