Commit Graph

348 Commits

Author SHA1 Message Date
Ben Thorner
8502827afb Handle errors when registration fails
Previously we would raise a 500 error in a variety of cases:

- If a second key was being registered simultaneously (e.g. in a
separate tab), which means the registration state could be missing
after the first registration completes. That smells like an attack.

- If the server-side verification failed e.g. origin verification,
challenge verification, etc. The library seems to use 'ValueError'
for all such errors [1] (after auditing its 'raise' statements, and
excluding AttestationError [2], since we're not doing that).

- If a key is used that attempts to sign with an unsupported
algorithm. This would normally raise a NotImplemented error as part
of verifying attestation [3], but we don't do that, so we need to
verify the algorithm is supported by the library manually.

This adds error handling to return a 400 response and error message
in these cases, since the error is not unexpected (i.e. not a 500).
A 400 seems more appropriate than a 403, since in many cases it's
not clear if the request data is valid.

I've used CBOR for the transport encoding, to match the successful
request / response encoding. Note that the ordering of then/catch
matters in JS - we don't want to catch our own throws!

[1]: 142587b3e6/fido2/server.py (L255)
[2]: c42d9628a4/fido2/attestation/base.py (L39)
[3]: c42d9628a4/fido2/cose.py (L92)
2021-05-17 12:18:24 +01:00
Katie Smith
bafcc02b7d Integrate with the API for adding and getting webauthn creds
This links up the `get_webauthn_credentials_for_user` and
`create_webauthn_credential_for_user` methods of the user api client to
notifications-api.

To send data to the API we need strings to be unicode, so we call
decode('utf-8') on base64 objects.

Co-authored-by: Leo Hemsted <leo.hemsted@digital.cabinet-office.gov.uk>
2021-05-14 14:28:24 +01:00
Pea Tyczynska
714afff156 Merge pull request #3884 from alphagov/add-webauthn-as-auth-type
Add webauthn as an auth type
2021-05-13 14:32:03 +01:00
Ben Thorner
e2cf3e2c70 Support registering a new authenticator
This adds Yubico's FIDO2 library and two APIs for working with the
"navigator.credentials.create()" function in JavaScript. The GET
API uses the library to generate options for the "create()" function,
and the POST API decodes and verifies the resulting credential. While
the options and response are dict-like, CBOR is necessary to encode
some of the byte-level values, which can't be represented in JSON.

Much of the code here is based on the Yubico library example [1][2].

Implementation notes:

- There are definitely better ways to alert the user about failure, but
window.alert() will do for the time being. Using location.reload() is
also a bit jarring if the page scrolls, but not a major issue.

- Ideally we would use window.fetch() to do AJAX calls, but we don't
have a polyfill for this, and we use $.ajax() elsewhere [3]. We need
to do a few weird tricks [6] to stop jQuery trashing the data.

- The FIDO2 server doesn't serve web requests; it's just a "server" in
the sense of WebAuthn terminology. It lives in its own module, since it
needs to be initialised with the app / config.

- $.ajax returns a promise-like object. Although we've used ".fail()"
elsewhere [3], I couldn't find a stub object that supports it, so I've
gone for ".catch()", and used a Promise stub object in tests.

- WebAuthn only works over HTTPS, but there's an exception for "localhost"
[4].  However, the library is a bit too strict [5], so we have to disable
origin verification to avoid needing HTTPS for dev work.

[1]: c42d9628a4/examples/server/server.py
[2]: c42d9628a4/examples/server/static/register.html
[3]: 91453d3639/app/assets/javascripts/updateContent.js (L33)
[4]: https://stackoverflow.com/questions/55971593/navigator-credentials-is-null-on-local-server
[5]: c42d9628a4/fido2/rpid.py (L69)
[6]: https://stackoverflow.com/questions/12394622/does-jquery-ajax-or-load-allow-for-responsetype-arraybuffer
2021-05-13 10:22:23 +01:00
Pea Tyczynska
2a756f90d4 Add webauth as an auth type
When showing what auth type user uses to sign in, add a text
for users with webauthn.

On password change or sign in, throw not implemented error
if user uses webauthn auth.
2021-05-12 17:40:31 +01:00
Ben Thorner
ebb82b2e80 Add page for security keys with stubbed data
This adds a new platform admin settings row, leading a page which
shows any existing keys and allows a new one to be registered. Until
the APIs for this are implemented, the user API client just returns
some stubbed data for manual testing.

This also includes a basic JavaScript module to do the main work of
registering a new authenticator, to be implemented in the next commits.

Some more minor notes:

- Setting the headings in the mapping_table is necessary to get the
horizontal rule along the top (to match the design).

- Setting caption to False in the mapping_table is necessary to stop
an extra margin appearing at the top.
2021-05-12 13:41:53 +01:00
Chris Hill-Scott
4da38e42e3 Make alert page work for rejected broadcasts
The code for this page was making assumptions about properties which
aren’t present on rejected broadcasts.

This commit accounts for those properties and presents the relevant
elements on the page.
2021-04-08 14:19:36 +01:00
David McDonald
4acca3de4d Don't show user as unknown for service history
A commit was added:
600e3affc1

In it, it falls back to the string 'Unknown' for actions done by those
not belonging to the service.

This commit changes the behaviour such that if the user is not in the
list of active users for a service, it will go get the user from the DB
(or redis). This should be fine to do as redis will protect us from most
calls as most of these cases are for platform admins.

This will mean we can now see which user platform admin put a service
live rather than seeing 'Unknown'.
2021-04-06 11:36:54 +01:00
Chris Hill-Scott
aa2a10c843 Merge pull request #3833 from alphagov/vary-bleed-by-population-density
Vary bleed amount based on population density
2021-03-22 10:03:19 +00:00
Chris Hill-Scott
6c8bfdc5b0 Refactor failed login count
We don’t vary this between different environments so it doesn’t need to
be in the config.

I was trying to look up what this value was and found it a bit confusing
that it was spread across multiple places.
2021-03-19 15:20:11 +00:00
Chris Hill-Scott
738ac1d818 Vary bleed amount based on population density
There are basically two kinds of 4G masts:

Frequency | Range       | Bandwidth
----------|-------------|----------------------------------
800MHz    | Long (500m) | Low (can handle a bit of traffic)
1800Mhz   | Short (5km) | High (can handle lots of traffic)

The 1800Mhz masts are better in terms of how much traffic they can
handle and how fast a connection they provide. But because they have
quite short range, it’s only economical to install them in very built up
areas†.

In more rural areas the 800MHz masts are better because they cover a
wider area, and have enough bandwidth for the lower population density.

The net effect of this is that cell broadcasts in rural areas are likely
to bleed further, because the masts they are being broadcast from are
less precise.

We can use population density as a proxy for how likely it is to be
covered by 1800Mhz masts, and therefore how much bleed we should expect.
So this commit varies the amount of bleed shown based on the population
density.

I came up with the formula based on 3 fixed points:
- The most remote areas (for example the Scottish Highlands) should have
  the highest average bleed, estimated at 5km
- An town, like Crewe, should have about the same bleed as we were
  estimating before (1.5km) – Pete D thinks this is about right based on
  his knowledge of the area around his office in Crewe
- The most built up areas, like London boroughs, could have as little as
  500m of bleed

Based on these three figures I came up with the following formula, which
roughly gives the right bleed distance (`b`) for each of their population
densities (`d`):
```
b = 5900 - (log10(d) × 1_250)
```

Plotted on a curve it looks like this:

This is based on averages – remember that the UI shows where is _likely_
to receive the alert, based on bleed, not where it’s _possible_ to
receive the alert.

Here’s what it looks like on the map:

---

†There are some additional subtleties which make this not strictly true:
- The 800Mhz masts are also used in built up areas to fill in the gaps
  between the areas covered by the 1800Mhz masts
- Switching between masts is inefficient, so if you’re moving fast
  through a built up area (for example on a train) your phone will only
  use the 800MHz masts so that you have to handoff from one mast to
  another less often
2021-03-18 09:37:23 +00:00
Katie Smith
776e24a215 Merge pull request #3840 from alphagov/accounts-page-fix
Fix /accounts page to only show trial services once
2021-03-17 15:29:07 +00:00
Leo Hemsted
8a2fec6f18 stop putting invite user objects in the session
the invited_user objects can be arbitrarily large, and when we put them
in the session we risk going over the session cookie's 4kb size limit.
since https://github.com/alphagov/notifications-admin/pull/3827 was
merged, we store the user id in the session. Now that's been live for a
day or two we can safely stop putting the rich object in the session.

Needed to change a bunch of tests for this to make sure appropriate
mocks were set. Also some tests were accidentally re-using fake_uuid.

Still pop the object when cleaning up sessions. We'll need to remove
that in a future PR.
2021-03-16 18:14:04 +00:00
Katie Smith
82733d615f Delete User properties which are now no longer used 2021-03-16 15:45:21 +00:00
Leo Hemsted
45297eae43 store invited user ids in session
same as the invited org user ids in the previous commit
2021-03-15 12:21:58 +00:00
Leo Hemsted
6d62c9ba36 store invited org user ids in session
first of a two step process to remove invited user objects from the
session. we're removing them because they're of variable size, and with
a lot of folder permissions they can cause the session to exceed the 4kb
cookie size limit and not save properly.

this commit looks at invited org users only.

in this step, start saving the invited org user's id to the
session alongside the session object. Then, if the invited_org_user_id
is present in the next step of the invite flow, fetch the user object
from the API instead of from the session. If it's not present (due to a
session set by an older instance of the admin app), then just use the
old code to get the entire object out of the session.

For invites where the user is small enough to persist to the cookie,
this will still save both the old and the new way, but will always make
an extra check to the API, I think this minor performance hit is totally
fine. For invites where the user is too big to persist, they'll still
fail for now, and will need to wait until the next PR comes along and
stops saving the large invited user object to the session entirely.
2021-03-12 16:36:02 +00:00
Leo Hemsted
c89be0079a rename get_invited_user funcs
make it clear they're expecting a service/org id
2021-03-12 15:59:32 +00:00
Katie Smith
7ae4017d50 Add audit event for inviting users to a service 2021-03-08 14:34:50 +00:00
David McDonald
f8f3d44511 Add form to set service broadcast account type
Note, no option at the moment to set the service broadcast account type
as None, or back to without the broadcast permission. This has been done
for speed of development given the chance of us needing this is very
low. We can add it later if we need to.
2021-02-23 16:03:14 +00:00
David McDonald
6837b76d44 Remove existing broadcast permission form
This will be replaced by a new form that has it's own template, route
etc as it will vary quite a lot from the existing service permission
form.
2021-02-23 16:03:13 +00:00
Pea Tyczynska
e090d97997 View edit organisation notes page
Also fix page title for edit service notes page.
2021-02-16 14:46:12 +00:00
Pea Tyczynska
56b777872e Organisation billing details visible on organisation settings page 2021-02-16 14:46:12 +00:00
Pea Tyczynska
f0d94a009e Organisation notes visible on organisation settings page 2021-02-16 14:46:11 +00:00
Chris Hill-Scott
276644f504 Reduce default broadcast expiry time to 4 hours
We don’t think we need to broadcast longer than this to validate that
the system is working.
2021-02-12 17:43:11 +00:00
Chris Hill-Scott
769b85ff25 Replace polygons module with the one from utils
We moved it in https://github.com/alphagov/notifications-utils/pull/818/files
2021-02-12 14:52:53 +00:00
Katie Smith
c9234bcf72 Allow broadcasts which have no created_at to be compared
This adds to the `__le__` method on the `BroadcastMessage` class to
allow two BroadcastMessages with no `updated_at` to be compared.
Previously, the method expected at least one message to have
`updated_at` set, but this is not necessarily the case.
2021-02-09 09:32:07 +00:00
Pea Tyczynska
079f371bc5 Update attribute names following changes in API 2021-02-03 10:30:01 +00:00
Pea Tyczynska
4a7bfcc9c5 Add billing details property
This property lets service settings know if there are any
billing details to preview.
2021-02-03 10:29:18 +00:00
Chris Hill-Scott
926ada2f21 Check that all areas are in the library
We should assume to start with that areas come either from the library
or from the JSON, and not a combination of the two.
2021-01-27 14:32:35 +00:00
Chris Hill-Scott
5ae53b625b Show broadcasts created by the API
Broadcasts created by the API are different in that:
- they aren’t created by any user, so don’t have a `created_by_id`
- they are created instantly, not in steps, so don’t have an
  `updated_at` time

This commit alters the views to account for when these pieces of
information aren’t present.
2021-01-27 11:34:43 +00:00
Chris Hill-Scott
60aa2d2b42 Display areas that aren’t in the library 2021-01-26 10:49:47 +00:00
Pea Tyczynska
e2ecff050d Merge pull request #3772 from alphagov/add-service-notes
Add service notes
2021-01-20 17:19:22 +00:00
Chris Hill-Scott
db4d9f886c Display broadcasts without a template
At the moment the admin app expects all broadcasts to have a template,
and expects the content of the alert to come from the template.

This commit makes it so those pages can still get a `Template` instance,
but populated with content straight from the `content` field in the
database.
2021-01-20 11:18:13 +00:00
Chris Hill-Scott
99b7d8a66f Add flow for composing an alert without a template
We think that in some cases alerts will be composed in the moment, and
therefore making people first create a template is:
- not a good use of their time
- adding some conceptual complexity which they don’t need

This commit makes it possible to type some words and have them go
straight into the `content` field in the database.

In the future we might want to progressively enhance the radio buttons
so they show on the same page (like we do with the grey buttons on the
templates page).
2021-01-18 17:09:01 +00:00
Pea Tyczynska
89b3f6ace2 Display notes section in Service Settings 2021-01-13 18:48:20 +00:00
Chris Hill-Scott
64835cba86 Merge pull request #3475 from alphagov/request-invite
Allow invite form to be pre-populated with an existing user
2021-01-08 11:16:18 +00:00
Chris Hill-Scott
7a95e1618e Extract formatters into their own module
We have lots of functions for converting various types of data into
strings to be displayed to the user somewhere.

This commit collects all these functions into their own module, rather
than having them cluttering up `app/__init__.py` or buried amongst
various other things that have ended up in `app/utils.py`.
2021-01-07 11:57:30 +00:00
Chris Hill-Scott
2c46d023da Extract Spreadsheet model from app/utils.py
`app/utils.py` is a bit of a dumping ground for things we don’t have a
better place for.

We now have a place and structure for storing ‘model’ code (‘model’ in
the model, view, controller (MVC) sense of the word).

This commit moves the spreadsheet model to that place.
2021-01-06 12:31:39 +00:00
Chris Hill-Scott
a68dfae5c2 Short circuit if team member is already invited
It would be confusing if people got invited twice, so let’s tell people
if someone’s has a pending invite.
2020-12-31 14:50:13 +00:00
Leo Hemsted
c805e7bd9c show allowed broadcast provider
in the "training"/"live" window
2020-12-04 10:52:09 +00:00
Chris Hill-Scott
b579f68411 Limit jobs sent from contact list by data retention
On the uploads page we only show jobs which are within a service’s data
retention.

This commit does the same for when we’re listing the jobs for a contact
list. This matches the UI, which says a contact list has been ‘used
`<count_of_jobs>` in the last <data_retention> days’
2020-12-01 13:57:56 +00:00
Chris Hill-Scott
45b60e9555 Show usage count on uploads page
Because we’re be grouping jobs under their parent contact lists it’s
good to have some information ‘scent’ to help people find their jobs,
ie by clicking into a contact list. It also lets you see which list have
been used more than others, maybe because the update hasn’t been sent
to that group of people yet.

The hint text under uploads always says when they were used. For contact
lists this is a bit more complicated, since they can:
- never have been used
- been used multiple times

This commit makes use of the new fields being returned by the API to say
determine when these messages are relevant. They also let us
differentiate between a contact list that’s never been used, and one
that has been used, but not recently enough to show any jobs against it.
2020-11-30 13:54:54 +00:00
Chris Hill-Scott
423875011c Show jobs on contact list
It’s a bit unintuitive that starting a job from a contact list makes a
copy of the file, which has no relationship to the list it was copied
from. This is more of an implementation detail, rather than something
that comes from people’s mental models of what is going on. Or at least
that’s what I hypothesise.

I think it’s clearer to show jobs that come from contact lists within
the lists that they were created from. By naming the jobs by template
this gives a clearer view of what messages have been sent to the group
over time.
2020-11-30 13:54:54 +00:00
Chris Hill-Scott
8f5c07336d Add method to get jobs for a contact list
Because the API lets us query jobs by contact list now, let’s add the
model and client layer code that will let us display them in the admin
app.
2020-11-30 13:54:53 +00:00
Chris Hill-Scott
236ddf053d Make created_at a property of contact lists
The view layer shouldn’t be having to deal with converting dates as
strings. This is an artefact of how we send data from the API to the
admin app. The model layer should be responsible for turning JSON into
richer types, where it can.
2020-11-30 13:54:53 +00:00
Chris Hill-Scott
fc1ca3ab2f Refactor to make pagination reusable
The responses we get to paginated queries from the API are fairly
consistent, so we should be able to reuse the code that takes JSON
from the API and turns it into Python objects. This commits factors out
that code so that it is reusable (by inheriting from it).
2020-11-30 13:54:53 +00:00
Chris Hill-Scott
b6e100448d Merge pull request #3687 from alphagov/remove-custom-getattr
Remove custom `getattr` implementation from JSONModel
2020-10-28 10:46:13 +00:00
Chris Hill-Scott
38e9e84f77 Raise exception when overriding a custom property
If a subclass of `JSONModel` defines a property then we shouldn’t try
to override it with the value from the underlying dictionary.

Rather than silently fail we should raise an exception because it will
help keep our list of `ALLOWED_PROPERTIES` nice and tidy.
2020-10-28 10:08:45 +00:00
Chris Hill-Scott
e2e04b51fc Sort broadcasts by start time
For emails and text messages we sort by the time the user (or API) sent
them.

This makes sense for broadcasts too, since most users will receive the
alert within seconds of it being broadcast.

For alerts that haven’t started yet we can sort by `updated_at`, which
is when the user preparing the broadcast submitted it for approval.
2020-10-27 13:12:46 +00:00
Chris Hill-Scott
57fc209f44 Merge pull request #3690 from alphagov/set-message-limit
Remove the `upload_letters` permission
2020-10-23 12:27:28 +01:00