scramp


Namescramp JSON
Version 1.4.5 PyPI version JSON
download
home_pageNone
SummaryAn implementation of the SCRAM protocol.
upload_time2024-04-13 12:35:19
maintainerNone
docs_urlNone
authorThe Contributors
requires_python>=3.8
licenseMIT No Attribution
keywords sasl scram authentication
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Scramp

A Python implementation of the [SCRAM authentication protocol](
https://en.wikipedia.org/wiki/Salted_Challenge_Response_Authentication_Mechanism>).
Scramp supports the following mechanisms:

- SCRAM-SHA-1
- SCRAM-SHA-1-PLUS
- SCRAM-SHA-256
- SCRAM-SHA-256-PLUS
- SCRAM-SHA-512
- SCRAM-SHA-512-PLUS
- SCRAM-SHA3-512
- SCRAM-SHA3-512-PLUS


## Installation

- Create a virtual environment: `python3 -m venv venv`
- Activate the virtual environment: `source venv/bin/activate`
- Install: `pip install scramp`


## Examples

### Client and Server

Here's an example using both the client and the server. It's a bit contrived as normally
you'd be using either the client or server on its own.

```python
>>> from scramp import ScramClient, ScramMechanism
>>>
>>> USERNAME = 'user'
>>> PASSWORD = 'pencil'
>>> MECHANISMS = ['SCRAM-SHA-256']
>>>
>>>
>>> # Choose a mechanism for our server
>>> m = ScramMechanism()  # Default is SCRAM-SHA-256
>>>
>>> # On the server side we create the authentication information for each user
>>> # and store it in an authentication database. We'll use a dict:
>>> db = {}
>>>
>>> salt, stored_key, server_key, iteration_count = m.make_auth_info(PASSWORD)
>>>
>>> db[USERNAME] = salt, stored_key, server_key, iteration_count
>>>
>>> # Define your own function for retrieving the authentication information
>>> # from the database given a username
>>>
>>> def auth_fn(username):
...     return db[username]
>>>
>>> # Make the SCRAM server
>>> s = m.make_server(auth_fn)
>>>
>>> # Now set up the client and carry out authentication with the server
>>> c = ScramClient(MECHANISMS, USERNAME, PASSWORD)
>>> cfirst = c.get_client_first()
>>>
>>> s.set_client_first(cfirst)
>>> sfirst = s.get_server_first()
>>>
>>> c.set_server_first(sfirst)
>>> cfinal = c.get_client_final()
>>>
>>> s.set_client_final(cfinal)
>>> sfinal = s.get_server_final()
>>>
>>> c.set_server_final(sfinal)
>>>
>>> # If it all runs through without raising an exception, the authentication
>>> # has succeeded
```


### Client only

Here's an example using just the client. The client nonce is specified in order to give
a reproducible example, but in production you'd omit the `c_nonce` parameter and let
`ScramClient` generate a client nonce:

```python
>>> from scramp import ScramClient
>>>
>>> USERNAME = 'user'
>>> PASSWORD = 'pencil'
>>> C_NONCE = 'rOprNGfwEbeRWgbNEkqO'
>>> MECHANISMS = ['SCRAM-SHA-256']
>>>
>>> # Normally the c_nonce would be omitted, in which case ScramClient will
>>> # generate the nonce itself.
>>>
>>> c = ScramClient(MECHANISMS, USERNAME, PASSWORD, c_nonce=C_NONCE)
>>>
>>> # Get the client first message and send it to the server
>>> cfirst = c.get_client_first()
>>> print(cfirst)
n,,n=user,r=rOprNGfwEbeRWgbNEkqO
>>>
>>> # Set the first message from the server
>>> c.set_server_first(
...     'r=rOprNGfwEbeRWgbNEkqO%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0,'
...     's=W22ZaJ0SNY7soEsUEjb6gQ==,i=4096')
>>>
>>> # Get the client final message and send it to the server
>>> cfinal = c.get_client_final()
>>> print(cfinal)
c=biws,r=rOprNGfwEbeRWgbNEkqO%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0,p=dHzbZapWIk4jUhN+Ute9ytag9zjfMHgsqmmiz7AndVQ=
>>>
>>> # Set the final message from the server
>>> c.set_server_final('v=6rriTRBi23WpRR/wtup+mMhUZUn/dB5nLTJRsjl95G4=')
>>>
>>> # If it all runs through without raising an exception, the authentication
>>> # has succeeded
```


### Server only

Here's an example using just the server. The server nonce and salt is specified in order
to give a reproducible example, but in production you'd omit the `s_nonce` and `salt`
parameters and let Scramp generate them:

```python
>>> from scramp import ScramMechanism
>>>
>>> USERNAME = 'user'
>>> PASSWORD = 'pencil'
>>> S_NONCE = '%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0'
>>> SALT = b'[m\x99h\x9d\x125\x8e\xec\xa0K\x14\x126\xfa\x81'
>>>
>>> db = {}
>>>
>>> m = ScramMechanism()
>>>
>>> salt, stored_key, server_key, iteration_count = m.make_auth_info(
...     PASSWORD, salt=SALT)
>>>
>>> db[USERNAME] = salt, stored_key, server_key, iteration_count
>>>
>>> # Define your own function for getting a password given a username
>>> def auth_fn(username):
...     return db[username]
>>>
>>> # Normally the s_nonce parameter would be omitted, in which case the
>>> # server will generate the nonce itself.
>>>
>>> s = m.make_server(auth_fn, s_nonce=S_NONCE)
>>>
>>> # Set the first message from the client
>>> s.set_client_first('n,,n=user,r=rOprNGfwEbeRWgbNEkqO')
>>>
>>> # Get the first server message, and send it to the client
>>> sfirst = s.get_server_first()
>>> print(sfirst)
r=rOprNGfwEbeRWgbNEkqO%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0,s=W22ZaJ0SNY7soEsUEjb6gQ==,i=4096
>>>
>>> # Set the final message from the client
>>> s.set_client_final(
...     'c=biws,r=rOprNGfwEbeRWgbNEkqO%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0,'
...     'p=dHzbZapWIk4jUhN+Ute9ytag9zjfMHgsqmmiz7AndVQ=')
>>>
>>> # Get the final server message and send it to the client
>>> sfinal = s.get_server_final()
>>> print(sfinal)
v=6rriTRBi23WpRR/wtup+mMhUZUn/dB5nLTJRsjl95G4=
>>>
>>> # If it all runs through without raising an exception, the authentication
>>> # has succeeded
```


### Server only with passlib

Here's an example using just the server and using the [passlib hashing library](
https://passlib.readthedocs.io/en/stable/index.html). The server nonce and salt is
specified in order to give a reproducible example, but in production you'd omit the
`s_nonce` and `salt` parameters and let Scramp generate them:

```python
>>> from scramp import ScramMechanism
>>> from passlib.hash import scram
>>>
>>> USERNAME = 'user'
>>> PASSWORD = 'pencil'
>>> S_NONCE = '%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0'
>>> SALT = b'[m\x99h\x9d\x125\x8e\xec\xa0K\x14\x126\xfa\x81'
>>> ITERATION_COUNT = 4096
>>>
>>> db = {}
>>> hash = scram.using(salt=SALT, rounds=ITERATION_COUNT).hash(PASSWORD)
>>>
>>> salt, iteration_count, digest = scram.extract_digest_info(hash, 'sha-256')
>>> 
>>> stored_key, server_key = m.make_stored_server_keys(digest)
>>>
>>> db[USERNAME] = salt, stored_key, server_key, iteration_count
>>>
>>> # Define your own function for getting a password given a username
>>> def auth_fn(username):
...     return db[username]
>>>
>>> # Normally the s_nonce parameter would be omitted, in which case the
>>> # server will generate the nonce itself.
>>>
>>> m = ScramMechanism()
>>> s = m.make_server(auth_fn, s_nonce=S_NONCE)
>>>
>>> # Set the first message from the client
>>> s.set_client_first('n,,n=user,r=rOprNGfwEbeRWgbNEkqO')
>>>
>>> # Get the first server message, and send it to the client
>>> sfirst = s.get_server_first()
>>> print(sfirst)
r=rOprNGfwEbeRWgbNEkqO%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0,s=W22ZaJ0SNY7soEsUEjb6gQ==,i=4096
>>>
>>> # Set the final message from the client
>>> s.set_client_final(
...     'c=biws,r=rOprNGfwEbeRWgbNEkqO%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0,'
...     'p=dHzbZapWIk4jUhN+Ute9ytag9zjfMHgsqmmiz7AndVQ=')
>>>
>>> # Get the final server message and send it to the client
>>> sfinal = s.get_server_final()
>>> print(sfinal)
v=6rriTRBi23WpRR/wtup+mMhUZUn/dB5nLTJRsjl95G4=
>>>
>>> # If it all runs through without raising an exception, the authentication
>>> # has succeeded
```


### Server Error

Here's an example of when setting a message from the client causes an error. The server
nonce and salt is specified in order to give a reproducible example, but in production
you'd omit the `s_nonce` and `salt` parameters and let Scramp generate them:

```python
>>> from scramp import ScramException, ScramMechanism
>>>
>>> USERNAME = 'user'
>>> PASSWORD = 'pencil'
>>> S_NONCE = '%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0'
>>> SALT = b'[m\x99h\x9d\x125\x8e\xec\xa0K\x14\x126\xfa\x81'
>>>
>>> db = {}
>>>
>>> m = ScramMechanism()
>>>
>>> salt, stored_key, server_key, iteration_count = m.make_auth_info(
...     PASSWORD, salt=SALT)
>>>
>>> db[USERNAME] = salt, stored_key, server_key, iteration_count
>>>
>>> # Define your own function for getting a password given a username
>>> def auth_fn(username):
...     return db[username]
>>>
>>> # Normally the s_nonce parameter would be omitted, in which case the
>>> # server will generate the nonce itself.
>>>
>>> s = m.make_server(auth_fn, s_nonce=S_NONCE)
>>>
>>> try:
...     # Set the first message from the client
...     s.set_client_first('p=tls-unique,,n=user,r=rOprNGfwEbeRWgbNEkqO')
... except ScramException as e:
...     print(e)
...     # Get the final server message and send it to the client
...     sfinal = s.get_server_final()
...     print(sfinal)
Received GS2 flag 'p' which indicates that the client requires channel binding, but the server does not: channel-binding-not-supported
e=channel-binding-not-supported

```


### Standards

- [RFC 5802](https://tools.ietf.org/html/rfc5802>) Describes SCRAM.
- [RFC 7677](https://datatracker.ietf.org/doc/html/rfc7677>) Registers SCRAM-SHA-256 and SCRAM-SHA-256-PLUS.
- [draft-melnikov-scram-sha-512-02](https://datatracker.ietf.org/doc/html/draft-melnikov-scram-sha-512) Registers SCRAM-SHA-512 and SCRAM-SHA-512-PLUS.
- [draft-melnikov-scram-sha3-512](https://datatracker.ietf.org/doc/html/draft-melnikov-scram-sha3-512) Registers SCRAM-SHA3-512 and SCRAM-SHA3-512-PLUS.
- [RFC 5929](https://datatracker.ietf.org/doc/html/rfc5929) Channel Bindings for TLS.
- [draft-ietf-kitten-tls-channel-bindings-for-tls13](https://datatracker.ietf.org/doc/html/draft-ietf-kitten-tls-channel-bindings-for-tls13>) Defines the `tls-exporter` channel binding, which is [not yet supported by Scramp](https://github.com/tlocke/scramp/issues/9).


## API Docs


### scramp.MECHANISMS

A tuple of the supported mechanism names.


### scramp.ScramClient

`ScramClient(mechanisms, username, password, channel_binding=None, c_nonce=None)`

Constructor of the `scramp.ScramClient` class, with the following parameters:

- `mechanisms` - A list or tuple of mechanism names. ScramClient will choose the most secure. If `cbind_data` is `None`, the '-PLUS' variants will be filtered out first. The chosen mechanism is available as the property `mechanism_name`.
- `username`
- `password`
- `channel_binding` - Providing a value for this parameter allows channel binding to be used (ie. it lets you use mechanisms ending in '-PLUS'). The value for `channel_binding` is a tuple consisting of the channel binding name and the channel binding data. For example, if the channel binding name is `tls-unique`, the `channel_binding` parameter would be `('tls-unique', data)`, where `data` is obtained by calling [SSLSocket.get\_channel\_binding()](https://docs.python.org/3/library/ssl.html#ssl.SSLSocket.get_channel_binding). The convenience function `scramp.make_channel_binding()` can be used to create a channel binding tuple.
- `c_nonce` - The client nonce. It's sometimes useful to set this when testing / debugging, but in production this should be omitted, in which case `ScramClient` will generate a client nonce.

The `ScramClient` object has the following methods and properties:

- `get_client_first()` - Get the client first message.
- `set_server_first(message)` - Set the first message from the server.
- `get_client_final()` - Get the final client message.
- `set_server_final(message)` - Set the final message from the server.
- `mechanism_name` - The mechanism chosen from the list given in the constructor.


### scramp.ScramMechanism

`ScramMechanism(mechanism='SCRAM-SHA-256')`

Constructor of the `ScramMechanism` class, with the following parameter:

- `mechanism` - The SCRAM mechanism to use.

The `ScramMechanism` object has the following methods and properties:

- `make_auth_info(password, iteration_count=None, salt=None)` - Returns the tuple `(salt, stored_key, server_key, iteration_count)` which is stored in the authentication database on the server side. It has the following parameters:
  - `password` - The user's password as a `str`.
  - `iteration_count` - The rounds as an `int`. If `None` then use the minimum associated with the mechanism.
  - `salt` - It's sometimes useful to set this binary parameter when testing / debugging, but in production this should be omitted, in which case a salt will be generated.
- `make_server(auth_fn, channel_binding=None, s_nonce=None)` - returns a `ScramServer` object. It takes the following parameters:
  - `auth_fn` This is a function provided by the programmer that has one parameter, a username of type `str` and returns returns the tuple `(salt, stored_key, server_key, iteration_count)`. Where `salt`, `stored_key` and `server_key` are of a binary type, and `iteration_count` is an `int`.
  - `channel_binding` - Providing a value for this parameter allows channel binding to be used (ie.  it lets you use mechanisms ending in `-PLUS`). The value for `channel_binding` is a tuple consisting of the channel binding name and the channel binding data. For example, if the channel binding name is 'tls-unique', the `channel_binding` parameter would be `('tls-unique', data)`, where `data` is obtained by calling [SSLSocket.get\_channel\_binding()](https://docs.python.org/3/library/ssl.html#ssl.SSLSocket.get_channel_binding>). The convenience function `scramp.make_channel_binding()` can be used to create a channel binding tuple. If `channel_binding` is provided and the mechanism isn't a `-PLUS` variant, then the server will negotiate with the client to use the `-PLUS` variant if the client supports it, or otherwise to use the mechanism without channel binding.
  - `s_nonce` - The server nonce as a `str`. It's sometimes useful to set this when testing / debugging, but in production this should be omitted, in which case `ScramServer` will generate a server nonce.
- `make_stored_server_keys(salted_password)` - Returns `(stored_key, server_key)` tuple of `bytes` objects given a salted password. This is useful if you want to use a separate hashing implementation from the one provided by Scramp. It takes the following parameter:
  - `salted_password` - A binary object representing the hashed password.
- `iteration_count` - The minimum iteration count recommended for this mechanism.


### scramp.ScramServer

The `ScramServer` object has the following methods:

- `set_client_first(message)` - Set the first message from the client.
- `get_server_first()` - Get the server first message.
- `set_client_final(message)` - Set the final client message.
- `get_server_final()` - Get the server final message.


### scramp.make\_channel\_binding(name, ssl\_socket)

A helper function that makes a `channel_binding` tuple when given a channel binding name and an SSL socket. The parameters are:
- `name` - A channel binding name such as 'tls-unique' or 'tls-server-end-point'.
- `ssl_socket` - An instance of [ssl.SSLSocket](https://docs.python.org/3/library/ssl.html#ssl.SSLSocket).


## Testing

- Activate the virtual environment: `source venv/bin/activate`
- Install `tox`: `pip install tox`
- Run `tox`: `tox`


## OpenSSF Scorecard

It might be worth running the [OpenSSF Scorecard](https://securityscorecards.dev/):

`sudo docker run -e GITHUB_AUTH_TOKEN=<auth_token> gcr.io/openssf/scorecard:stable --repo=github.com/tlocke/scramp`


## Doing A Release Of Scramp

Run `tox` to make sure all tests pass, then update the release notes, then do:

- `git tag -a x.y.z -m "version x.y.z"`
- `rm -r dist`
- `python -m build`
- `twine upload dist/*`


## Release Notes

### Version 1.4.5, 2024-04-13

- Drop support for Python 3.7, which means we're not dependent on `importlib-metadata` anymore.
- Various changes to build system, docs and automated testing.


### Version 1.4.4, 2022-11-01

- Tighten up parsing of messages to make sure that a `ScramException` is raised if a message is malformed.


### Version 1.4.3, 2022-10-26

- The client now sends a gs2-cbind-flag of 'y' if the client supports channel binding, but thinks the server does not.


### Version 1.4.2, 2022-10-22

- Switch to using the MIT-0 licence https://choosealicense.com/licenses/mit-0/
- When creating a ScramClient, allow non ``-PLUS`` variants, even if a `channel_binding` parameter is provided. Previously this would raise and exception.


### Version 1.4.1, 2021-08-25

- When using `make_channel_binding()` to create a tls-server-end-point channel binding, support certificates with hash algorithm of sha512.


### Version 1.4.0, 2021-03-28

- Raise an exception if the client receives an error from the server.


### Version 1.3.0, 2021-03-28

- As the specification allows, server errors are now sent to the client in the `server_final` message, an exception is still thrown as before.


### Version 1.2.2, 2021-02-13

- Fix bug in generating the AuthMessage. It was incorrect when channel binding was used. So now Scramp supports channel binding.


### Version 1.2.1, 2021-02-07

- Add support for channel binding.
- Add support for SCRAM-SHA-512 and SCRAM-SHA3-512 and their channel binding variants.


### Version 1.2.0, 2020-05-30

- This is a backwardly incompatible change on the server side, the client side will work as before. The idea of this change is to make it possible to have an authentication database. That is, the authentication information can be stored, and then retrieved when needed to authenticate the user.
- In addition, it's now possible on the server side to use a third party hashing library such as passlib as the hashing implementation.


### Version 1.1.1, 2020-03-28

- Add the README and LICENCE to the distribution.


### Version 1.1.0, 2019-02-24

- Add support for the SCRAM-SHA-1 mechanism.


### Version 1.0.0, 2019-02-17

- Implement the server side as well as the client side.


### Version 0.0.0, 2019-02-10

- Copied SCRAM implementation from [pg8000](https://github.com/tlocke/pg8000). The idea is to make it a general SCRAM implemtation. Credit to the [Scrampy](https://github.com/cagdass/scrampy) project which I read through to help with this project. Also credit to the [passlib](https://github.com/efficks/passlib) project from which I copied the `saslprep` function.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "scramp",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "SASL, SCRAM, authentication",
    "author": "The Contributors",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/f9/fa/8f1b99c3f875f334ac782e173ec03c35c246ec7a94fc5dd85153bc1d8285/scramp-1.4.5.tar.gz",
    "platform": null,
    "description": "# Scramp\n\nA Python implementation of the [SCRAM authentication protocol](\nhttps://en.wikipedia.org/wiki/Salted_Challenge_Response_Authentication_Mechanism>).\nScramp supports the following mechanisms:\n\n- SCRAM-SHA-1\n- SCRAM-SHA-1-PLUS\n- SCRAM-SHA-256\n- SCRAM-SHA-256-PLUS\n- SCRAM-SHA-512\n- SCRAM-SHA-512-PLUS\n- SCRAM-SHA3-512\n- SCRAM-SHA3-512-PLUS\n\n\n## Installation\n\n- Create a virtual environment: `python3 -m venv venv`\n- Activate the virtual environment: `source venv/bin/activate`\n- Install: `pip install scramp`\n\n\n## Examples\n\n### Client and Server\n\nHere's an example using both the client and the server. It's a bit contrived as normally\nyou'd be using either the client or server on its own.\n\n```python\n>>> from scramp import ScramClient, ScramMechanism\n>>>\n>>> USERNAME = 'user'\n>>> PASSWORD = 'pencil'\n>>> MECHANISMS = ['SCRAM-SHA-256']\n>>>\n>>>\n>>> # Choose a mechanism for our server\n>>> m = ScramMechanism()  # Default is SCRAM-SHA-256\n>>>\n>>> # On the server side we create the authentication information for each user\n>>> # and store it in an authentication database. We'll use a dict:\n>>> db = {}\n>>>\n>>> salt, stored_key, server_key, iteration_count = m.make_auth_info(PASSWORD)\n>>>\n>>> db[USERNAME] = salt, stored_key, server_key, iteration_count\n>>>\n>>> # Define your own function for retrieving the authentication information\n>>> # from the database given a username\n>>>\n>>> def auth_fn(username):\n...     return db[username]\n>>>\n>>> # Make the SCRAM server\n>>> s = m.make_server(auth_fn)\n>>>\n>>> # Now set up the client and carry out authentication with the server\n>>> c = ScramClient(MECHANISMS, USERNAME, PASSWORD)\n>>> cfirst = c.get_client_first()\n>>>\n>>> s.set_client_first(cfirst)\n>>> sfirst = s.get_server_first()\n>>>\n>>> c.set_server_first(sfirst)\n>>> cfinal = c.get_client_final()\n>>>\n>>> s.set_client_final(cfinal)\n>>> sfinal = s.get_server_final()\n>>>\n>>> c.set_server_final(sfinal)\n>>>\n>>> # If it all runs through without raising an exception, the authentication\n>>> # has succeeded\n```\n\n\n### Client only\n\nHere's an example using just the client. The client nonce is specified in order to give\na reproducible example, but in production you'd omit the `c_nonce` parameter and let\n`ScramClient` generate a client nonce:\n\n```python\n>>> from scramp import ScramClient\n>>>\n>>> USERNAME = 'user'\n>>> PASSWORD = 'pencil'\n>>> C_NONCE = 'rOprNGfwEbeRWgbNEkqO'\n>>> MECHANISMS = ['SCRAM-SHA-256']\n>>>\n>>> # Normally the c_nonce would be omitted, in which case ScramClient will\n>>> # generate the nonce itself.\n>>>\n>>> c = ScramClient(MECHANISMS, USERNAME, PASSWORD, c_nonce=C_NONCE)\n>>>\n>>> # Get the client first message and send it to the server\n>>> cfirst = c.get_client_first()\n>>> print(cfirst)\nn,,n=user,r=rOprNGfwEbeRWgbNEkqO\n>>>\n>>> # Set the first message from the server\n>>> c.set_server_first(\n...     'r=rOprNGfwEbeRWgbNEkqO%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0,'\n...     's=W22ZaJ0SNY7soEsUEjb6gQ==,i=4096')\n>>>\n>>> # Get the client final message and send it to the server\n>>> cfinal = c.get_client_final()\n>>> print(cfinal)\nc=biws,r=rOprNGfwEbeRWgbNEkqO%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0,p=dHzbZapWIk4jUhN+Ute9ytag9zjfMHgsqmmiz7AndVQ=\n>>>\n>>> # Set the final message from the server\n>>> c.set_server_final('v=6rriTRBi23WpRR/wtup+mMhUZUn/dB5nLTJRsjl95G4=')\n>>>\n>>> # If it all runs through without raising an exception, the authentication\n>>> # has succeeded\n```\n\n\n### Server only\n\nHere's an example using just the server. The server nonce and salt is specified in order\nto give a reproducible example, but in production you'd omit the `s_nonce` and `salt`\nparameters and let Scramp generate them:\n\n```python\n>>> from scramp import ScramMechanism\n>>>\n>>> USERNAME = 'user'\n>>> PASSWORD = 'pencil'\n>>> S_NONCE = '%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0'\n>>> SALT = b'[m\\x99h\\x9d\\x125\\x8e\\xec\\xa0K\\x14\\x126\\xfa\\x81'\n>>>\n>>> db = {}\n>>>\n>>> m = ScramMechanism()\n>>>\n>>> salt, stored_key, server_key, iteration_count = m.make_auth_info(\n...     PASSWORD, salt=SALT)\n>>>\n>>> db[USERNAME] = salt, stored_key, server_key, iteration_count\n>>>\n>>> # Define your own function for getting a password given a username\n>>> def auth_fn(username):\n...     return db[username]\n>>>\n>>> # Normally the s_nonce parameter would be omitted, in which case the\n>>> # server will generate the nonce itself.\n>>>\n>>> s = m.make_server(auth_fn, s_nonce=S_NONCE)\n>>>\n>>> # Set the first message from the client\n>>> s.set_client_first('n,,n=user,r=rOprNGfwEbeRWgbNEkqO')\n>>>\n>>> # Get the first server message, and send it to the client\n>>> sfirst = s.get_server_first()\n>>> print(sfirst)\nr=rOprNGfwEbeRWgbNEkqO%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0,s=W22ZaJ0SNY7soEsUEjb6gQ==,i=4096\n>>>\n>>> # Set the final message from the client\n>>> s.set_client_final(\n...     'c=biws,r=rOprNGfwEbeRWgbNEkqO%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0,'\n...     'p=dHzbZapWIk4jUhN+Ute9ytag9zjfMHgsqmmiz7AndVQ=')\n>>>\n>>> # Get the final server message and send it to the client\n>>> sfinal = s.get_server_final()\n>>> print(sfinal)\nv=6rriTRBi23WpRR/wtup+mMhUZUn/dB5nLTJRsjl95G4=\n>>>\n>>> # If it all runs through without raising an exception, the authentication\n>>> # has succeeded\n```\n\n\n### Server only with passlib\n\nHere's an example using just the server and using the [passlib hashing library](\nhttps://passlib.readthedocs.io/en/stable/index.html). The server nonce and salt is\nspecified in order to give a reproducible example, but in production you'd omit the\n`s_nonce` and `salt` parameters and let Scramp generate them:\n\n```python\n>>> from scramp import ScramMechanism\n>>> from passlib.hash import scram\n>>>\n>>> USERNAME = 'user'\n>>> PASSWORD = 'pencil'\n>>> S_NONCE = '%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0'\n>>> SALT = b'[m\\x99h\\x9d\\x125\\x8e\\xec\\xa0K\\x14\\x126\\xfa\\x81'\n>>> ITERATION_COUNT = 4096\n>>>\n>>> db = {}\n>>> hash = scram.using(salt=SALT, rounds=ITERATION_COUNT).hash(PASSWORD)\n>>>\n>>> salt, iteration_count, digest = scram.extract_digest_info(hash, 'sha-256')\n>>> \n>>> stored_key, server_key = m.make_stored_server_keys(digest)\n>>>\n>>> db[USERNAME] = salt, stored_key, server_key, iteration_count\n>>>\n>>> # Define your own function for getting a password given a username\n>>> def auth_fn(username):\n...     return db[username]\n>>>\n>>> # Normally the s_nonce parameter would be omitted, in which case the\n>>> # server will generate the nonce itself.\n>>>\n>>> m = ScramMechanism()\n>>> s = m.make_server(auth_fn, s_nonce=S_NONCE)\n>>>\n>>> # Set the first message from the client\n>>> s.set_client_first('n,,n=user,r=rOprNGfwEbeRWgbNEkqO')\n>>>\n>>> # Get the first server message, and send it to the client\n>>> sfirst = s.get_server_first()\n>>> print(sfirst)\nr=rOprNGfwEbeRWgbNEkqO%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0,s=W22ZaJ0SNY7soEsUEjb6gQ==,i=4096\n>>>\n>>> # Set the final message from the client\n>>> s.set_client_final(\n...     'c=biws,r=rOprNGfwEbeRWgbNEkqO%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0,'\n...     'p=dHzbZapWIk4jUhN+Ute9ytag9zjfMHgsqmmiz7AndVQ=')\n>>>\n>>> # Get the final server message and send it to the client\n>>> sfinal = s.get_server_final()\n>>> print(sfinal)\nv=6rriTRBi23WpRR/wtup+mMhUZUn/dB5nLTJRsjl95G4=\n>>>\n>>> # If it all runs through without raising an exception, the authentication\n>>> # has succeeded\n```\n\n\n### Server Error\n\nHere's an example of when setting a message from the client causes an error. The server\nnonce and salt is specified in order to give a reproducible example, but in production\nyou'd omit the `s_nonce` and `salt` parameters and let Scramp generate them:\n\n```python\n>>> from scramp import ScramException, ScramMechanism\n>>>\n>>> USERNAME = 'user'\n>>> PASSWORD = 'pencil'\n>>> S_NONCE = '%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0'\n>>> SALT = b'[m\\x99h\\x9d\\x125\\x8e\\xec\\xa0K\\x14\\x126\\xfa\\x81'\n>>>\n>>> db = {}\n>>>\n>>> m = ScramMechanism()\n>>>\n>>> salt, stored_key, server_key, iteration_count = m.make_auth_info(\n...     PASSWORD, salt=SALT)\n>>>\n>>> db[USERNAME] = salt, stored_key, server_key, iteration_count\n>>>\n>>> # Define your own function for getting a password given a username\n>>> def auth_fn(username):\n...     return db[username]\n>>>\n>>> # Normally the s_nonce parameter would be omitted, in which case the\n>>> # server will generate the nonce itself.\n>>>\n>>> s = m.make_server(auth_fn, s_nonce=S_NONCE)\n>>>\n>>> try:\n...     # Set the first message from the client\n...     s.set_client_first('p=tls-unique,,n=user,r=rOprNGfwEbeRWgbNEkqO')\n... except ScramException as e:\n...     print(e)\n...     # Get the final server message and send it to the client\n...     sfinal = s.get_server_final()\n...     print(sfinal)\nReceived GS2 flag 'p' which indicates that the client requires channel binding, but the server does not: channel-binding-not-supported\ne=channel-binding-not-supported\n\n```\n\n\n### Standards\n\n- [RFC 5802](https://tools.ietf.org/html/rfc5802>) Describes SCRAM.\n- [RFC 7677](https://datatracker.ietf.org/doc/html/rfc7677>) Registers SCRAM-SHA-256 and SCRAM-SHA-256-PLUS.\n- [draft-melnikov-scram-sha-512-02](https://datatracker.ietf.org/doc/html/draft-melnikov-scram-sha-512) Registers SCRAM-SHA-512 and SCRAM-SHA-512-PLUS.\n- [draft-melnikov-scram-sha3-512](https://datatracker.ietf.org/doc/html/draft-melnikov-scram-sha3-512) Registers SCRAM-SHA3-512 and SCRAM-SHA3-512-PLUS.\n- [RFC 5929](https://datatracker.ietf.org/doc/html/rfc5929) Channel Bindings for TLS.\n- [draft-ietf-kitten-tls-channel-bindings-for-tls13](https://datatracker.ietf.org/doc/html/draft-ietf-kitten-tls-channel-bindings-for-tls13>) Defines the `tls-exporter` channel binding, which is [not yet supported by Scramp](https://github.com/tlocke/scramp/issues/9).\n\n\n## API Docs\n\n\n### scramp.MECHANISMS\n\nA tuple of the supported mechanism names.\n\n\n### scramp.ScramClient\n\n`ScramClient(mechanisms, username, password, channel_binding=None, c_nonce=None)`\n\nConstructor of the `scramp.ScramClient` class, with the following parameters:\n\n- `mechanisms` - A list or tuple of mechanism names. ScramClient will choose the most secure. If `cbind_data` is `None`, the '-PLUS' variants will be filtered out first. The chosen mechanism is available as the property `mechanism_name`.\n- `username`\n- `password`\n- `channel_binding` - Providing a value for this parameter allows channel binding to be used (ie. it lets you use mechanisms ending in '-PLUS'). The value for `channel_binding` is a tuple consisting of the channel binding name and the channel binding data. For example, if the channel binding name is `tls-unique`, the `channel_binding` parameter would be `('tls-unique', data)`, where `data` is obtained by calling [SSLSocket.get\\_channel\\_binding()](https://docs.python.org/3/library/ssl.html#ssl.SSLSocket.get_channel_binding). The convenience function `scramp.make_channel_binding()` can be used to create a channel binding tuple.\n- `c_nonce` - The client nonce. It's sometimes useful to set this when testing / debugging, but in production this should be omitted, in which case `ScramClient` will generate a client nonce.\n\nThe `ScramClient` object has the following methods and properties:\n\n- `get_client_first()` - Get the client first message.\n- `set_server_first(message)` - Set the first message from the server.\n- `get_client_final()` - Get the final client message.\n- `set_server_final(message)` - Set the final message from the server.\n- `mechanism_name` - The mechanism chosen from the list given in the constructor.\n\n\n### scramp.ScramMechanism\n\n`ScramMechanism(mechanism='SCRAM-SHA-256')`\n\nConstructor of the `ScramMechanism` class, with the following parameter:\n\n- `mechanism` - The SCRAM mechanism to use.\n\nThe `ScramMechanism` object has the following methods and properties:\n\n- `make_auth_info(password, iteration_count=None, salt=None)` - Returns the tuple `(salt, stored_key, server_key, iteration_count)` which is stored in the authentication database on the server side. It has the following parameters:\n  - `password` - The user's password as a `str`.\n  - `iteration_count` - The rounds as an `int`. If `None` then use the minimum associated with the mechanism.\n  - `salt` - It's sometimes useful to set this binary parameter when testing / debugging, but in production this should be omitted, in which case a salt will be generated.\n- `make_server(auth_fn, channel_binding=None, s_nonce=None)` - returns a `ScramServer` object. It takes the following parameters:\n  - `auth_fn` This is a function provided by the programmer that has one parameter, a username of type `str` and returns returns the tuple `(salt, stored_key, server_key, iteration_count)`. Where `salt`, `stored_key` and `server_key` are of a binary type, and `iteration_count` is an `int`.\n  - `channel_binding` - Providing a value for this parameter allows channel binding to be used (ie.  it lets you use mechanisms ending in `-PLUS`). The value for `channel_binding` is a tuple consisting of the channel binding name and the channel binding data. For example, if the channel binding name is 'tls-unique', the `channel_binding` parameter would be `('tls-unique', data)`, where `data` is obtained by calling [SSLSocket.get\\_channel\\_binding()](https://docs.python.org/3/library/ssl.html#ssl.SSLSocket.get_channel_binding>). The convenience function `scramp.make_channel_binding()` can be used to create a channel binding tuple. If `channel_binding` is provided and the mechanism isn't a `-PLUS` variant, then the server will negotiate with the client to use the `-PLUS` variant if the client supports it, or otherwise to use the mechanism without channel binding.\n  - `s_nonce` - The server nonce as a `str`. It's sometimes useful to set this when testing / debugging, but in production this should be omitted, in which case `ScramServer` will generate a server nonce.\n- `make_stored_server_keys(salted_password)` - Returns `(stored_key, server_key)` tuple of `bytes` objects given a salted password. This is useful if you want to use a separate hashing implementation from the one provided by Scramp. It takes the following parameter:\n  - `salted_password` - A binary object representing the hashed password.\n- `iteration_count` - The minimum iteration count recommended for this mechanism.\n\n\n### scramp.ScramServer\n\nThe `ScramServer` object has the following methods:\n\n- `set_client_first(message)` - Set the first message from the client.\n- `get_server_first()` - Get the server first message.\n- `set_client_final(message)` - Set the final client message.\n- `get_server_final()` - Get the server final message.\n\n\n### scramp.make\\_channel\\_binding(name, ssl\\_socket)\n\nA helper function that makes a `channel_binding` tuple when given a channel binding name and an SSL socket. The parameters are:\n- `name` - A channel binding name such as 'tls-unique' or 'tls-server-end-point'.\n- `ssl_socket` - An instance of [ssl.SSLSocket](https://docs.python.org/3/library/ssl.html#ssl.SSLSocket).\n\n\n## Testing\n\n- Activate the virtual environment: `source venv/bin/activate`\n- Install `tox`: `pip install tox`\n- Run `tox`: `tox`\n\n\n## OpenSSF Scorecard\n\nIt might be worth running the [OpenSSF Scorecard](https://securityscorecards.dev/):\n\n`sudo docker run -e GITHUB_AUTH_TOKEN=<auth_token> gcr.io/openssf/scorecard:stable --repo=github.com/tlocke/scramp`\n\n\n## Doing A Release Of Scramp\n\nRun `tox` to make sure all tests pass, then update the release notes, then do:\n\n- `git tag -a x.y.z -m \"version x.y.z\"`\n- `rm -r dist`\n- `python -m build`\n- `twine upload dist/*`\n\n\n## Release Notes\n\n### Version 1.4.5, 2024-04-13\n\n- Drop support for Python 3.7, which means we're not dependent on `importlib-metadata` anymore.\n- Various changes to build system, docs and automated testing.\n\n\n### Version 1.4.4, 2022-11-01\n\n- Tighten up parsing of messages to make sure that a `ScramException` is raised if a message is malformed.\n\n\n### Version 1.4.3, 2022-10-26\n\n- The client now sends a gs2-cbind-flag of 'y' if the client supports channel binding, but thinks the server does not.\n\n\n### Version 1.4.2, 2022-10-22\n\n- Switch to using the MIT-0 licence https://choosealicense.com/licenses/mit-0/\n- When creating a ScramClient, allow non ``-PLUS`` variants, even if a `channel_binding` parameter is provided. Previously this would raise and exception.\n\n\n### Version 1.4.1, 2021-08-25\n\n- When using `make_channel_binding()` to create a tls-server-end-point channel binding, support certificates with hash algorithm of sha512.\n\n\n### Version 1.4.0, 2021-03-28\n\n- Raise an exception if the client receives an error from the server.\n\n\n### Version 1.3.0, 2021-03-28\n\n- As the specification allows, server errors are now sent to the client in the `server_final` message, an exception is still thrown as before.\n\n\n### Version 1.2.2, 2021-02-13\n\n- Fix bug in generating the AuthMessage. It was incorrect when channel binding was used. So now Scramp supports channel binding.\n\n\n### Version 1.2.1, 2021-02-07\n\n- Add support for channel binding.\n- Add support for SCRAM-SHA-512 and SCRAM-SHA3-512 and their channel binding variants.\n\n\n### Version 1.2.0, 2020-05-30\n\n- This is a backwardly incompatible change on the server side, the client side will work as before. The idea of this change is to make it possible to have an authentication database. That is, the authentication information can be stored, and then retrieved when needed to authenticate the user.\n- In addition, it's now possible on the server side to use a third party hashing library such as passlib as the hashing implementation.\n\n\n### Version 1.1.1, 2020-03-28\n\n- Add the README and LICENCE to the distribution.\n\n\n### Version 1.1.0, 2019-02-24\n\n- Add support for the SCRAM-SHA-1 mechanism.\n\n\n### Version 1.0.0, 2019-02-17\n\n- Implement the server side as well as the client side.\n\n\n### Version 0.0.0, 2019-02-10\n\n- Copied SCRAM implementation from [pg8000](https://github.com/tlocke/pg8000). The idea is to make it a general SCRAM implemtation. Credit to the [Scrampy](https://github.com/cagdass/scrampy) project which I read through to help with this project. Also credit to the [passlib](https://github.com/efficks/passlib) project from which I copied the `saslprep` function.\n",
    "bugtrack_url": null,
    "license": "MIT No Attribution",
    "summary": "An implementation of the SCRAM protocol.",
    "version": "1.4.5",
    "project_urls": {
        "Homepage": "https://github.com/tlocke/scramp"
    },
    "split_keywords": [
        "sasl",
        " scram",
        " authentication"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d99f8b2f2749ccfbe4fcef08650896ac47ed919ff25b7ac57b7a1ae7da16c8c3",
                "md5": "95ff4349358113345ce47c476f371b04",
                "sha256": "50e37c464fc67f37994e35bee4151e3d8f9320e9c204fca83a5d313c121bbbe7"
            },
            "downloads": -1,
            "filename": "scramp-1.4.5-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "95ff4349358113345ce47c476f371b04",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 12781,
            "upload_time": "2024-04-13T12:35:17",
            "upload_time_iso_8601": "2024-04-13T12:35:17.466457Z",
            "url": "https://files.pythonhosted.org/packages/d9/9f/8b2f2749ccfbe4fcef08650896ac47ed919ff25b7ac57b7a1ae7da16c8c3/scramp-1.4.5-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f9fa8f1b99c3f875f334ac782e173ec03c35c246ec7a94fc5dd85153bc1d8285",
                "md5": "f843217a4c0a85c01a4ceec970c3aa6e",
                "sha256": "be3fbe774ca577a7a658117dca014e5d254d158cecae3dd60332dfe33ce6d78e"
            },
            "downloads": -1,
            "filename": "scramp-1.4.5.tar.gz",
            "has_sig": false,
            "md5_digest": "f843217a4c0a85c01a4ceec970c3aa6e",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 16169,
            "upload_time": "2024-04-13T12:35:19",
            "upload_time_iso_8601": "2024-04-13T12:35:19.617018Z",
            "url": "https://files.pythonhosted.org/packages/f9/fa/8f1b99c3f875f334ac782e173ec03c35c246ec7a94fc5dd85153bc1d8285/scramp-1.4.5.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-13 12:35:19",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "tlocke",
    "github_project": "scramp",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "scramp"
}
        
Elapsed time: 0.23259s