Skip to content

Commit

Permalink
Add an admin endpoint to check a session_code (#4234)
Browse files Browse the repository at this point in the history
  • Loading branch information
nono authored Nov 27, 2023
2 parents a4998a3 + 584eba8 commit 11860ab
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
31 changes: 31 additions & 0 deletions docs/admin.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,37 @@ Content-Type: application/json
}
```

### POST /instances/:domain/session_code/check

Checks that a session_code is valid for the given instance. Note that the
session_code will be invalidated after that.

#### Request

```http
POST /instances/alice.cozy.localhost/session_code/check HTTP/1.1
Content-Type: application/json
```

```json
{
"session_code": "L7oJ6BDQtdbLR5Vr5vTxTXLJ1pQzMXcD"
}
```

#### Response

```http
HTTP/1.1 200 OK
Content-Type: application/json
```

```json
{
"valid": true
}
```

### POST /instances/:domain/email_verified_code

Creates an email_verified_code that can be used on the given instance to avoid
Expand Down
28 changes: 28 additions & 0 deletions web/instances/instances.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Package instances is used for the admin endpoint to manage instances. It
// covers a lot of things, from creating an instance to checking the FS
// integrity.
package instances

import (
Expand Down Expand Up @@ -379,6 +382,30 @@ func createSessionCode(c echo.Context) error {
})
}

type checkSessionCodeArgs struct {
Code string `json:"session_code"`
}

func checkSessionCode(c echo.Context) error {
domain := c.Param("domain")
inst, err := lifecycle.GetInstance(domain)
if err != nil {
return err
}

var args checkSessionCodeArgs
if err := c.Bind(&args); err != nil {
return err
}

ok := inst.CheckAndClearSessionCode(args.Code)
if !ok {
return c.JSON(http.StatusForbidden, echo.Map{"valid": false})
}

return c.JSON(http.StatusOK, echo.Map{"valid": true})
}

func createEmailVerifiedCode(c echo.Context) error {
domain := c.Param("domain")
inst, err := lifecycle.GetInstance(domain)
Expand Down Expand Up @@ -684,6 +711,7 @@ func Routes(router *echo.Group) {
router.POST("/:domain/auth-mode", setAuthMode)
router.POST("/:domain/magic_link", createMagicLink)
router.POST("/:domain/session_code", createSessionCode)
router.POST("/:domain/session_code/check", checkSessionCode)
router.POST("/:domain/email_verified_code", createEmailVerifiedCode)
router.DELETE("/:domain/sessions", cleanSessions)

Expand Down

0 comments on commit 11860ab

Please sign in to comment.