Skip to content

Commit

Permalink
endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
tlangs committed Oct 13, 2023
1 parent 0a36476 commit 8da07d8
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 2 deletions.
86 changes: 86 additions & 0 deletions src/main/resources/swagger/api-docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2793,6 +2793,54 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/ErrorReport'
/api/users/v2/self:
get:
tags:
- Users
summary: gets the user
operationId: getSamUserSelf
responses:
200:
description: user exists
content:
application/json:
schema:
$ref: '#/components/schemas/SamUser'
404:
description: user not found
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorReport'
/api/users/v2/{sam_user_id}:
get:
tags:
- Users
summary: gets a user
description: Gets a SamUser by their id. This endpoint is scoped to the permissions of the caller.
A normal user can call the endpoint with their own id, but trying to get another user will result in a 404.
Admin permissions grant the caller the ability to get any id.
operationId: getSamUserById
parameters:
- name: sam_user_id
in: path
description: the id of the sam user to get
required: true
schema:
type: string
responses:
200:
description: user exists
content:
application/json:
schema:
$ref: '#/components/schemas/SamUser'
404:
description: user not found
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorReport'
/register/user/v1:
get:
tags:
Expand Down Expand Up @@ -3799,6 +3847,44 @@ components:
format: date-time
description: User's time of last update
description: specification of a User
SamUser:
type: object
required:
- id
- email
- enabled
- createdAt
- updatedAt
properties:
id:
type: string
description: User's Id
googleSubjectId:
type: string
description: User's Google subject Id
email:
type: string
description: User's email address
format: email
azureB2CId:
type: string
description: User's Azure B2C Id
enabled:
type: boolean
description: Whether or not the user is enabled
createdAt:
type: string
format: date-time
description: User's time of creation
registeredAt:
type: string
format: date-time
description: User's time of registration
updatedAt:
type: string
format: date-time
description: User's time of last update
description: specification of a User
UpdateUserRequest:
type: object
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ abstract class SamRoutes(
with ManagedGroupRoutes
with AdminRoutes
with AzureRoutes
with ServiceAdminRoutes {
with ServiceAdminRoutes
with UserRoutesV3 {

def route: server.Route = (logRequestResult & handleExceptions(myExceptionHandler)) {
oidcConfig.swaggerRoutes("swagger/api-docs.yaml") ~
Expand All @@ -76,7 +77,8 @@ abstract class SamRoutes(
extensionRoutes(samUser, samRequestContextWithUser) ~
groupRoutes(samUser, samRequestContextWithUser) ~
apiUserRoutes(samUser, samRequestContextWithUser) ~
azureRoutes(samUser, samRequestContextWithUser)
azureRoutes(samUser, samRequestContextWithUser) ~
userRoutesV3(samUser, samRequestContextWithUser)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.broadinstitute.dsde.workbench.sam.api

import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.model.StatusCodes.{NotFound, OK}
import akka.http.scaladsl.server
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.{Directive0, ExceptionHandler}
import org.broadinstitute.dsde.workbench.model._
import org.broadinstitute.dsde.workbench.sam.model.api.SamJsonSupport._
import org.broadinstitute.dsde.workbench.sam.model.api.SamUser
import org.broadinstitute.dsde.workbench.sam.service.UserService
import org.broadinstitute.dsde.workbench.sam.util.SamRequestContext

/** Created by tlangs on 10/12/2023.
*/
trait UserRoutesV3 extends SamUserDirectives with SamRequestContextDirectives {
val userService: UserService

/** Changes a 403 error to a 404 error. Used when `UserInfoDirectives` throws a 403 in the case where a user is not found. In most routes that is appropriate
* but in the user routes it should be a 404.
*/
private val changeForbiddenToNotFound: Directive0 = {
import org.broadinstitute.dsde.workbench.model.ErrorReportJsonSupport._

handleExceptions(ExceptionHandler {
case withErrorReport: WorkbenchExceptionWithErrorReport if withErrorReport.errorReport.statusCode.contains(StatusCodes.Forbidden) =>
complete((StatusCodes.NotFound, withErrorReport.errorReport.copy(statusCode = Option(StatusCodes.NotFound))))
})
}

def userRoutesV3(samUser: SamUser, samRequestContext: SamRequestContext): server.Route =
pathPrefix("users") {
pathPrefix("v2") {
pathPrefix("self") {
pathEndOrSingleSlash {
get {
complete {
StatusCodes.OK -> samUser
}
}
}
} ~
pathPrefix(Segment) { samUserId =>
pathEndOrSingleSlash {
val workbenchUserId = WorkbenchUserId(samUserId)
if (workbenchUserId.equals(samUser.id)) {
get {
complete {
StatusCodes.OK -> samUser
}
}
} else {
(changeForbiddenToNotFound & asWorkbenchAdmin(samUser)) {
get {
complete {
userService.getUser(WorkbenchUserId(samUserId), samRequestContext).map(user => (if (user.isDefined) OK else NotFound) -> user)
}
}
}
}
}
}
}
}
}

0 comments on commit 8da07d8

Please sign in to comment.