You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

webauthn.go 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package auth
  5. import (
  6. "encoding/base64"
  7. "errors"
  8. "net/http"
  9. "code.gitea.io/gitea/models/auth"
  10. user_model "code.gitea.io/gitea/models/user"
  11. wa "code.gitea.io/gitea/modules/auth/webauthn"
  12. "code.gitea.io/gitea/modules/base"
  13. "code.gitea.io/gitea/modules/context"
  14. "code.gitea.io/gitea/modules/log"
  15. "code.gitea.io/gitea/modules/setting"
  16. "code.gitea.io/gitea/services/externalaccount"
  17. "github.com/duo-labs/webauthn/protocol"
  18. "github.com/duo-labs/webauthn/webauthn"
  19. )
  20. var tplWebAuthn base.TplName = "user/auth/webauthn"
  21. // WebAuthn shows the WebAuthn login page
  22. func WebAuthn(ctx *context.Context) {
  23. ctx.Data["Title"] = ctx.Tr("twofa")
  24. // Check auto-login.
  25. if checkAutoLogin(ctx) {
  26. return
  27. }
  28. //Ensure user is in a 2FA session.
  29. if ctx.Session.Get("twofaUid") == nil {
  30. ctx.ServerError("UserSignIn", errors.New("not in WebAuthn session"))
  31. return
  32. }
  33. ctx.HTML(200, tplWebAuthn)
  34. }
  35. // WebAuthnLoginAssertion submits a WebAuthn challenge to the browser
  36. func WebAuthnLoginAssertion(ctx *context.Context) {
  37. // Ensure user is in a WebAuthn session.
  38. idSess, ok := ctx.Session.Get("twofaUid").(int64)
  39. if !ok || idSess == 0 {
  40. ctx.ServerError("UserSignIn", errors.New("not in WebAuthn session"))
  41. return
  42. }
  43. user, err := user_model.GetUserByID(idSess)
  44. if err != nil {
  45. ctx.ServerError("UserSignIn", err)
  46. return
  47. }
  48. exists, err := auth.ExistsWebAuthnCredentialsForUID(user.ID)
  49. if err != nil {
  50. ctx.ServerError("UserSignIn", err)
  51. return
  52. }
  53. if !exists {
  54. ctx.ServerError("UserSignIn", errors.New("no device registered"))
  55. return
  56. }
  57. assertion, sessionData, err := wa.WebAuthn.BeginLogin((*wa.User)(user), webauthn.WithAssertionExtensions(protocol.AuthenticationExtensions{
  58. "appid": setting.U2F.AppID,
  59. }))
  60. if err != nil {
  61. ctx.ServerError("webauthn.BeginLogin", err)
  62. return
  63. }
  64. if err := ctx.Session.Set("webauthnAssertion", sessionData); err != nil {
  65. ctx.ServerError("Session.Set", err)
  66. return
  67. }
  68. ctx.JSON(http.StatusOK, assertion)
  69. }
  70. // WebAuthnLoginAssertionPost validates the signature and logs the user in
  71. func WebAuthnLoginAssertionPost(ctx *context.Context) {
  72. idSess, ok := ctx.Session.Get("twofaUid").(int64)
  73. sessionData, okData := ctx.Session.Get("webauthnAssertion").(*webauthn.SessionData)
  74. if !ok || !okData || sessionData == nil || idSess == 0 {
  75. ctx.ServerError("UserSignIn", errors.New("not in WebAuthn session"))
  76. return
  77. }
  78. defer func() {
  79. _ = ctx.Session.Delete("webauthnAssertion")
  80. }()
  81. // Load the user from the db
  82. user, err := user_model.GetUserByID(idSess)
  83. if err != nil {
  84. ctx.ServerError("UserSignIn", err)
  85. return
  86. }
  87. log.Trace("Finishing webauthn authentication with user: %s", user.Name)
  88. // Now we do the equivalent of webauthn.FinishLogin using a combination of our session data
  89. // (from webauthnAssertion) and verify the provided request.0
  90. parsedResponse, err := protocol.ParseCredentialRequestResponse(ctx.Req)
  91. if err != nil {
  92. // Failed authentication attempt.
  93. log.Info("Failed authentication attempt for %s from %s: %v", user.Name, ctx.RemoteAddr(), err)
  94. ctx.Status(http.StatusForbidden)
  95. return
  96. }
  97. // Validate the parsed response.
  98. cred, err := wa.WebAuthn.ValidateLogin((*wa.User)(user), *sessionData, parsedResponse)
  99. if err != nil {
  100. // Failed authentication attempt.
  101. log.Info("Failed authentication attempt for %s from %s: %v", user.Name, ctx.RemoteAddr(), err)
  102. ctx.Status(http.StatusForbidden)
  103. return
  104. }
  105. // Ensure that the credential wasn't cloned by checking if CloneWarning is set.
  106. // (This is set if the sign counter is less than the one we have stored.)
  107. if cred.Authenticator.CloneWarning {
  108. log.Info("Failed authentication attempt for %s from %s: cloned credential", user.Name, ctx.RemoteAddr())
  109. ctx.Status(http.StatusForbidden)
  110. return
  111. }
  112. // Success! Get the credential and update the sign count with the new value we received.
  113. dbCred, err := auth.GetWebAuthnCredentialByCredID(base64.RawStdEncoding.EncodeToString(cred.ID))
  114. if err != nil {
  115. ctx.ServerError("GetWebAuthnCredentialByCredID", err)
  116. return
  117. }
  118. dbCred.SignCount = cred.Authenticator.SignCount
  119. if err := dbCred.UpdateSignCount(); err != nil {
  120. ctx.ServerError("UpdateSignCount", err)
  121. return
  122. }
  123. // Now handle account linking if that's requested
  124. if ctx.Session.Get("linkAccount") != nil {
  125. if err := externalaccount.LinkAccountFromStore(ctx.Session, user); err != nil {
  126. ctx.ServerError("LinkAccountFromStore", err)
  127. return
  128. }
  129. }
  130. remember := ctx.Session.Get("twofaRemember").(bool)
  131. redirect := handleSignInFull(ctx, user, remember, false)
  132. if redirect == "" {
  133. redirect = setting.AppSubURL + "/"
  134. }
  135. _ = ctx.Session.Delete("twofaUid")
  136. // Finally check if the appid extension was used:
  137. if value, ok := parsedResponse.ClientExtensionResults["appid"]; ok {
  138. if appid, ok := value.(bool); ok && appid {
  139. ctx.Flash.Error(ctx.Tr("webauthn_u2f_deprecated", dbCred.Name))
  140. }
  141. }
  142. ctx.JSON(200, map[string]string{"redirect": redirect})
  143. }