Using Roles: Functionality and Authorization - PuzzleServer/mainpuzzleserver GitHub Wiki

Functionality on the puzzle web site is governed by roles. A role is declared as a part of the URL (e.g. play in https://puzzlehunt.azurewebsites.net/pd2026/play) and then the use of the role must be authorized. Historically, there have been three primary roles, but two new roles have been added recently for special scenarios.

The current user role is exposed in EventSpecificPageModel.EventRole, and appropriate functionality can be delivered by a page based on that role. Before delivering functionality, however, the use of that role needs to be authorized, either by the use of an authorization attribute or by some runtime check. Some basic authorization is performed by EventSpecificPageModel.OnPageHandlerExecutionAsync(), but this is very general in nature and is usually insufficient. Authorization examples are listed for each role below.

Play

This role represents a player in the event. There is no need to authorize this role specifically, but certain authorizations related to player scenarios may be performed, such as:

  • [Authorize(Policy=IsRegisteredForEvent)]: looks at the event ID in the URL and makes sure the user is registered for it.
  • [Authorize(Policy=IsEventAdminOrPlayerOnTeam)]: looks at the team ID in the URL and makes sure the user is a member of that team, or is an admin of that event.
  • [Authorize(Policy=PlayerCanSeePuzzle)]: looks at the puzzle ID in the URL and verifies that the user has this puzzle unlocked.
  • PuzzleUser.IsRegisteredForEvent(): Checks for registered status at times post-authorization (necessary for scenarios like the menu bar)

Author

This role represents an author in the event, who may be marked as the author of zero or more puzzles. Common authorizations for authors are:

  • [Authorize(Policy=IsEventAdminOrEventAuthor)]: looks at the event ID in the URL and makes sure the user is on the list of authors for it, or is an admin for the event.
  • [Authorize(Policy=IsEventAdminOrAuthorOfPuzzle)]: looks at the puzzle ID in the URL and makes sure the user is either on the list of authors for it, or is an admin for the event.
  • PuzzleUser.IsAuthorForEvent(): Checks for author status at times post-authorization (necessary for scenarios like help threads)
  • UserEventHelper.IsAuthorOfPuzzle(): Checks for author status at times post-authorization (necessary for complex scenarios, like FilesController)

Admin

This role represents an administrator in the event. Administrators have the following types of access:

  • [Authorize(Policy=IsEventAdmin)]: looks at the event ID in the URL and makes sure the user is an admin of that event.
  • [Authorize(Policy=IsEventAdminOrPlayerOnTeam)]: looks at the team ID in the URL and makes sure the user is a member of that team, or is an admin of that event.
  • [Authorize(Policy=IsEventAdminOrEventAuthor)]: looks at the event ID in the URL and makes sure the user is on the list of authors for it, or is an admin for the event.
  • [Authorize(Policy=IsEventAdminOrAuthorOfPuzzle)]: looks at the puzzle ID in the URL and makes sure the user is either on the list of authors for it, or is an admin for the event.
  • PuzzleUser.IsAdminForEvent(): Checks for admin status at times post-authorization (necessary for scenarios like help threads)
  • UserEventHelper.IsAdminOfPuzzle(): Checks for admin status at times post-authorization (necessary for complex scenarios, like FilesController)

Impersonation

This role represents an author or admin temporarily impersonating a team for purposes of monitoring their progress. [There will also be a player impersonation role someday, but this is not yet implemented.]

The team impersonation role looks like impersonateteam-<teamid> and can be checked with EventRole.IsImpersonating. The Team ID is located in EventRole.ImpersonationId.

Most code does not need to know anything about impersonation roles; impersonation support is built into EventSpecificPageModel.GetTeamAsync() and EventSpecificPageModel.GetTeamId(). Those functions will get either the player's team or the team being impersonated.

Impersonation support is also automatically baked into these authorization checks:

  • [Authorize(Policy=IsEventAdminOrPlayerOnTeam)]: looks at the team ID in the URL and makes sure the user is a member of that team, or is an admin of that event.
  • [Authorize(Policy=PlayerCanSeePuzzle)]: looks at the puzzle ID in the URL and verifies that the user has this puzzle unlocked.

Archive

This role is used for generating a static archive of an event. By pointing a website copier at an event homepage in the archive role, all the web pages can be crawled and copied.

One wrinkle with the crawl/copy process is that there is no need or desire to have any user logged in. This means that any archivable page needs to either:

  • Have its [Authorize(Policy=...)] checks evaluate archive scenarios before considering the logged-in user.
  • Be anonymously accessible via the [AllowAnonymous] authorization attribute.

In particular, a plain [Authorize] attribute without policy is unworkable for archivable pages, since it performs a user check. If an archiving scenario requires that we change a page from a plain [Authorize] attribute to [AllowAnonymous], we have to be very careful that no unwanted access leaks through. Best practice is to add the following check very early in any page handler such as OnGetAsync():

// if we're not archiving, force users to identify themselves; the [Authorize] attribute does this automatically but we didn't use it here
if (EventRole != EventRole.archive && LoggedInUser == null)
{
    return Challenge();

    // don't forget to validate any other considerations for this user based on page needs
}

// probably unnecessary given EventSpecificPageModel.OnPageHandlerExecutionAsync but let's be safe
if (EventRole == EventRole.archive && !Event.CanArchive)
{
    return Forbid();
}

After this code, runtime checks must be added to support the proper authorization in non-archive scenarios (such as confirming that the player has the puzzle unlocked, etc.)

⚠️ **GitHub.com Fallback** ⚠️