Class SlackController

java.lang.Object
edu.ucsb.cs156.frontiers.controllers.ApiController
edu.ucsb.cs156.frontiers.controllers.SlackController

@RestController @RequestMapping("/api/courses/slack") public class SlackController extends ApiController
Endpoints for the Slack integration of a course.

The Slack bot token is a secret: it is encrypted at rest (using the same infrastructure as Canvas API tokens), never logged, and only ever returned to the frontend in masked form.

  • Field Details

  • Constructor Details

  • Method Details

    • getSlackInfo

      @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") @GetMapping("/info") public Map<String,String> getSlackInfo(@RequestParam Long courseId)
      Returns the Slack workspace (team) associated with the course, along with a masked version of the Slack bot token.
      Parameters:
      courseId - the id of the course
      Returns:
      a map with courseId, slackBotToken (masked), slackTeamId, slackTeamName and slackTeamUrl; values are empty strings when no token has been stored
    • updateSlackToken

      @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") @PostMapping("/token") public org.springframework.http.ResponseEntity<Map<String,Object>> updateSlackToken(@RequestParam Long courseId, @RequestParam String slackBotToken)
      Validates a Slack bot token by calling the Slack auth.test method. If the token is valid, it is encrypted and stored, along with the id and name of the workspace (team) it belongs to. If it is invalid, nothing is stored, and a 400 response describes the problem.

      The token should be sent in the (form encoded) body of the request rather than in the query string, so that it does not end up in access logs.

      Parameters:
      courseId - the id of the course
      slackBotToken - the Slack bot token (xoxb-...)
      Returns:
      on success, a map with ok=true, courseId, slackBotToken (masked), slackTeamId and slackTeamName; on failure, a map with ok=false, error (Slack error code) and message. If the token is valid but cannot be encrypted (e.g. the encryption key is not configured), it is not stored, and a 500 response with error ENCRYPTION_FAILED is returned.
    • slackTeamUrl

      public static String slackTeamUrl(Course course)
      The URL of the course's Slack workspace, e.g. https://ucsb-cs156-f26.slack.com/. Tokens stored before the URL was being recorded fall back to a URL based on the team id.
      Parameters:
      course - the course
      Returns:
      the URL, or an empty string if the course is not connected to a workspace
    • getSlackUsers

      @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") @GetMapping("/users") public List<SlackController.SlackUserView> getSlackUsers(@RequestParam Long courseId)
      Lists the people with active accounts in the course's Slack workspace (no bots, no deactivated accounts, no invitations that have not been accepted yet). Each is matched by email against the course to determine whether they are the instructor, a staff member, a roster student (only those with status ROSTER or MANUAL; not dropped students), or none of these.
      Parameters:
      courseId - the id of the course
      Returns:
      the active users of the workspace
    • getMissingMembers

      @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") @GetMapping("/missing") public List<SlackController.SlackMissingMemberView> getMissingMembers(@RequestParam Long courseId)
      Lists the roster students (only those with status ROSTER or MANUAL; not dropped students) and the staff of the course that do not have an active account in the course's Slack workspace, matching by email. For each, slackStatus indicates whether they have been invited but have not signed in yet, have a deactivated account, or are unknown to the workspace.
      Parameters:
      courseId - the id of the course
      Returns:
      staff first, then students
    • setupSectionChannels

      @PreAuthorize("@CourseSecurity.hasInstructorPermissions(#root, #courseId)") @PostMapping("/sectionChannels") public edu.ucsb.cs156.jobs.entities.Job setupSectionChannels(@RequestParam Long courseId)
      Launches a job that creates the public Slack channels named in the sections table of the course, adds the roster students of each section to its channel, and removes from those channels anybody who is neither a student of the section, nor staff, nor the instructor. See SetupSectionSlackChannelsJob.
      Parameters:
      courseId - the id of the course
      Returns:
      the job that was launched; its log can be seen on the Jobs tab of the course
    • setupTeamChannels

      @PreAuthorize("@CourseSecurity.hasInstructorPermissions(#root, #courseId)") @PostMapping("/teamChannels") public edu.ucsb.cs156.jobs.entities.Job setupTeamChannels(@RequestParam Long courseId)
      Launches a job that creates a public Slack channel for each team of the course (named "team-" followed by the team name in Slack's form), adds the members of each team and the instructor to the team's channel, and removes from those channels anybody who is neither on the team, nor the instructor, nor staff. See SetupTeamSlackChannelsJob.
      Parameters:
      courseId - the id of the course
      Returns:
      the job that was launched; its log can be seen on the Jobs tab of the course
    • setupPrivateChannels

      @PreAuthorize("@CourseSecurity.hasInstructorPermissions(#root, #courseId)") @PostMapping("/privateChannels") public edu.ucsb.cs156.jobs.entities.Job setupPrivateChannels(@RequestParam Long courseId)
      Launches a job that gives each roster student of the course who has a Slack account a private Slack channel (named "private-" followed by the first and last name of the student), and adds the student, the instructor and the staff of the course to it. A student's channel is recognized by its members rather than by its name, so running the job again creates no duplicates; it does remove from the existing channels anybody who is neither the student, nor the instructor, nor staff. See SetupPrivateSlackChannelsJob.
      Parameters:
      courseId - the id of the course
      Returns:
      the job that was launched; its log can be seen on the Jobs tab of the course
    • handleSlackApiException

      @ExceptionHandler(SlackApiException.class) public org.springframework.http.ResponseEntity<Map<String,Object>> handleSlackApiException(SlackApiException e)
      A failed call to Slack maps to a 502/Bad Gateway, with the Slack error code and a message.
      Parameters:
      e - the exception; its message is the Slack error code
      Returns:
      a map with ok=false, error (Slack error code) and message
    • maskToken

      public static String maskToken(String token)
      Masks a token so that only the first four and last four characters are visible. Tokens too short to mask meaningfully (eight characters or fewer) are masked entirely; Slack does not issue tokens that short.
      Parameters:
      token - the plaintext token
      Returns:
      the masked token, or an empty string if there is no token
    • encryptionFailedMessage

      public static String encryptionFailedMessage(String details)
      Message shown when a valid token could not be encrypted.
      Parameters:
      details - message from the encryption service, describing the problem with the key
      Returns:
      a human readable message
    • errorMessage

      public static String errorMessage(String error)
      Translates a Slack error code into a message suitable for showing to an instructor who has just tried to save a token.
      Parameters:
      error - error code from Slack, e.g. invalid_auth
      Returns:
      a human readable message
    • describeError

      public static String describeError(String error)
      Translates a Slack error code into a message suitable for showing to an instructor.
      Parameters:
      error - error code from Slack, e.g. invalid_auth
      Returns:
      a human readable message