Skip to content

Commit

Permalink
feat(api): Adding basic support for mobile authentication.
Browse files Browse the repository at this point in the history
Mobile clients will not be using an HTTP only cookie, instead they will
need to derive the token from the response directly and store it.

This adds `isMobile` as a valid field for the login request, and adds
parsing of the token header (with the same name as the cookie) to the
authentication middleware.
  • Loading branch information
elliotcourant committed Nov 5, 2022
1 parent bbb9b1d commit 89da686
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
33 changes: 22 additions & 11 deletions pkg/controller/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func (c *Controller) loginEndpoint(ctx iris.Context) {
Password string `json:"password"`
Captcha string `json:"captcha"`
TOTP string `json:"totp"`
IsMobile bool `json:"isMobile"`
}
if err := ctx.ReadJSON(&loginRequest); err != nil {
c.wrapAndReturnError(ctx, err, http.StatusBadRequest, "malformed json")
Expand Down Expand Up @@ -223,13 +224,19 @@ func (c *Controller) loginEndpoint(ctx iris.Context) {
return
}

c.updateAuthenticationCookie(ctx, token)
result := map[string]interface{}{
"isActive": true,
}

if !loginRequest.IsMobile {
c.updateAuthenticationCookie(ctx, token)
} else {
result["token"] = token
}

if !c.configuration.Stripe.IsBillingEnabled() {
// Return their account token.
ctx.JSON(map[string]interface{}{
"isActive": true,
})
ctx.JSON(result)
return
}

Expand All @@ -239,9 +246,7 @@ func (c *Controller) loginEndpoint(ctx iris.Context) {
return
}

result := map[string]interface{}{
"isActive": subscriptionIsActive,
}
result["isActive"] = subscriptionIsActive

if !subscriptionIsActive {
result["nextUrl"] = "/account/subscribe"
Expand All @@ -258,11 +263,17 @@ func (c *Controller) loginEndpoint(ctx iris.Context) {
return
}

c.updateAuthenticationCookie(ctx, token)

ctx.JSON(map[string]interface{}{
result := map[string]interface{}{
"users": login.Users,
})
}

if !loginRequest.IsMobile {
c.updateAuthenticationCookie(ctx, token)
} else {
result["token"] = token
}

ctx.JSON(result)
}
}

Expand Down
10 changes: 8 additions & 2 deletions pkg/controller/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ func (c *Controller) authenticateUser(ctx iris.Context) (err error) {
defer func() {
var message string
if err == nil {
message = "Token is valid"
message = "Auth is valid"
data["accountId"] = c.mustGetAccountId(ctx)
data["userId"] = c.mustGetUserId(ctx)
} else {
message = "Request did not have valid Token"
message = "Request did not have valid auth"
}

hub.AddBreadcrumb(&sentry.Breadcrumb{
Expand Down Expand Up @@ -126,6 +126,12 @@ func (c *Controller) authenticateUser(ctx iris.Context) (err error) {
}
}

if token != "" {
if token = ctx.GetHeader(c.configuration.Server.Cookies.Name); token != "" {
data["source"] = "header"
}
}

if token == "" {
return errors.New("token must be provided")
}
Expand Down

0 comments on commit 89da686

Please sign in to comment.