Development/OAuth2: Difference between revisions

From MusicBrainz Wiki
Jump to navigationJump to search
No edit summary
Line 80: Line 80:
''The specification for MAC token is not yet complete, so this authentication method might change as the specification develops. MusicBrainz implementation is based on revision 01 of the specification draft and only supports HMAC-SHA1 signatures.''
''The specification for MAC token is not yet complete, so this authentication method might change as the specification develops. MusicBrainz implementation is based on revision 01 of the specification draft and only supports HMAC-SHA1 signatures.''


MAC tokens consist of a public access token and a secret key associated with the token. They require application developers to send signed requests and can be used over both HTTP and HTTPS, because the access token is not considered a secret alone.
MAC tokens consist of a public access token and a secret key associated with the token. They require application developers to send signed requests and can be used over both HTTP and HTTPS, because the access token alone is not considered a secret.


First, the application needs to get the current timestamp and a short random string that should be unique for the timestamp called nonce. Let's assume we have timestamp <code>1336363200</code> and nonce <code>dj83hs9s</code>. We need to calculate a [http://en.wikipedia.org/wiki/Hash-based_message_authentication_code HMAC] signature of the request, which is done using a normalized request string.
First the application needs to get the current timestamp and a random string called nonce that should be unique for the timestamp. Let's assume we have timestamp <code>1336363200</code> and nonce <code>dj83hs9s</code>. We need to calculate a [http://en.wikipedia.org/wiki/Hash-based_message_authentication_code HMAC] signature of the request, which is done using a normalized request string as defined by the MAC authentication specification.


1336363200\n
1336363200\n

Revision as of 20:27, 27 October 2012

OAuth2 is a protocol that lets external applications request authentication of the user and authorization to perform actions using the web service on their behalf without getting their password. It is an alternative to HTTP Digest Access Authentication that MusicBrainz currently uses.

Basic steps

Registering an application

All developers need to register their application before getting started. A registered OAuth application is assigned a unique Client I and Client Secret. The OAuth protocol recognizes two types of applications:

  1. Confidential - Applications that can keep secrets. Typically web applications, running on a server.
  2. Public - Applications that have their code exposed to the public and therefore cannot keep secrets. There are typically installed desktop applications or web applications running in the browser.

Client Secret assigned to Confidential applications should not be shared. This does not apply to Public applications, in which case the assigned Client Secret is not treated as a secret and can be embedded in the application code.

Obtaining access token

...

Using access token

After an application has obtained an access token, it may use the token in the web service to get user details or submit data to MusicBrainz on the user's behalf. There are two types of access tokens:

  1. Bearer - Very easy to use, but can be used only over HTTPS.
  2. MAC - Requires requests to be signed with a secret, but can be used over both HTTP and HTTPS.

Refreshing access token

Access tokens have a limited life-time. During the authorization process, application receives a refresh token, in addition to the first access token. This refresh token allows applications to obtain new access tokens.

Authorization

Scopes

Authorization requests have a limited scope. You should request only the scopes that your application necessarily needs. The following scopes are available in the MusicBrainz OAuth implementation:

profile
View the user's public profile information (username, age, country, homepage).
email
View the user's email.
tag
View and modify the user's private tags.
rating
View and modify the user's private ratings.
collection
View and modify the user's private collections.
submit_puid
Submit new PUIDs to the database.
submit_isrc
Submit new ISRCs to the database.
submit_barcode
Submit barcodes to the database.

Access token usage

Bearer tokens

Bearer tokens are the default type of access tokens. They are very easy to use, consist only of one component, which you should treat as a password. For this reason, it is only possible to use them over HTTPS. If you try to send them over plain HTTP, they will be ignored.

The preferred method to use bearer tokens is via the Authorization header. An authenticated request would look like the following:

 GET /ws/2/user?name=xxx HTTP/1.1
 Host: musicbrainz.org
 Authorization: Bearer jr5xkCAg4hGcls9FXMVIuQ

If you can't use the Authorization header, there is an alternative in which you can pass the access token in the access_token query string parameter:

 GET https://musicbrainz.org/ws/2/user?name=xxx&access_token=jr5xkCAg4hGcls9FXMVIuQ

You can try it with the curl command line application:

 curl -H 'Authorization: Bearer jr5xkCAg4hGcls9FXMVIuQ' https://musicbrainz.org/ws/2/user?name=xxx

Or the query string parameter version:

 curl 'https://musicbrainz.org/ws/2/user?name=xxx&access_token=jr5xkCAg4hGcls9FXMVIuQ'

Bearer tokens are specified in RFC 6750.

MAC tokens

The specification for MAC token is not yet complete, so this authentication method might change as the specification develops. MusicBrainz implementation is based on revision 01 of the specification draft and only supports HMAC-SHA1 signatures.

MAC tokens consist of a public access token and a secret key associated with the token. They require application developers to send signed requests and can be used over both HTTP and HTTPS, because the access token alone is not considered a secret.

First the application needs to get the current timestamp and a random string called nonce that should be unique for the timestamp. Let's assume we have timestamp 1336363200 and nonce dj83hs9s. We need to calculate a HMAC signature of the request, which is done using a normalized request string as defined by the MAC authentication specification.

 1336363200\n
 dj83hs9s\n
 GET\n
 /ws/2/user?name=xxx\n
 musicbrainz.org\n
 80\n
 \n

Using this string as a message and the secret key assigned from the token, we calculate a base64 encoded signature using HMAC-SHA1, which in this example is MY2RO3VylIdLgFXx8bIdyce/544= if the secret key was yi3qjrMf4hG9VVUxXMVIuQ.

An authenticated request with access token jr5xkCAg4hGcls9FXMVIuQ would then look like the following:

 GET /ws/2/user?name=xxx HTTP/1.1
 Host: musicbrainz.org
 Authorization: MAC id="jr5xkCAg4hGcls9FXMVIuQ" ts="1336363200" nonce="dj83hs9s" mac="LGqwjo25phvhlgBr4b1NFE7EGmM="

If you want to use use this authentication method, please refer to the specification for details or use an OAuth 2.0 client library that has it already implemenmted.