1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-11-24 08:57:03 -05:00
forgejo/tests/integration/api_twofa_test.go

83 lines
2.1 KiB
Go
Raw Normal View History

// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package integration
import (
"net/http"
"testing"
"time"
auth_model "code.gitea.io/gitea/models/auth"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/tests"
"github.com/pquerna/otp/totp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAPITwoFactor(t *testing.T) {
defer tests.PrepareTestEnv(t)()
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 16})
req := NewRequest(t, "GET", "/api/v1/user").
AddBasicAuth(user.Name)
MakeRequest(t, req, http.StatusOK)
otpKey, err := totp.Generate(totp.GenerateOpts{
SecretSize: 40,
Issuer: "gitea-test",
AccountName: user.Name,
})
require.NoError(t, err)
tfa := &auth_model.TwoFactor{
UID: user.ID,
}
require.NoError(t, tfa.SetSecret(otpKey.Secret()))
require.NoError(t, auth_model.NewTwoFactor(db.DefaultContext, tfa))
req = NewRequest(t, "GET", "/api/v1/user").
AddBasicAuth(user.Name)
MakeRequest(t, req, http.StatusUnauthorized)
passcode, err := totp.GenerateCode(otpKey.Secret(), time.Now())
require.NoError(t, err)
req = NewRequest(t, "GET", "/api/v1/user").
AddBasicAuth(user.Name)
req.Header.Set("X-Gitea-OTP", passcode)
MakeRequest(t, req, http.StatusOK)
[BRANDING] X-Forgejo-OTP can be used instead of X-Gitea-OTP (cherry picked from commit 7b0549cd70aa7cafec853e15b25270847c59850b) (cherry picked from commit 13e10a65d974c7b594681bfa36402a6144862116) (cherry picked from commit 65bdd73cf27895a9fb8db2a95ef4f5b08951481d) (cherry picked from commit 64eba8bb923176b4c286b1d0c83792f3c3005ca8) (cherry picked from commit 4c49b1a759abe3604afc1121e83c9a942016ad6a) (cherry picked from commit 93b4d0640683ea986657453b1fce49a00c861764) (cherry picked from commit e2bc5f36d958f4349160ec145719c302d4023cd0) (cherry picked from commit 2bee76f9dfa998c83ea4fe648997fad0b6224fa9) (cherry picked from commit 3d8a1b4a9fb9dc55bbd62fd8855ea85e58dc263f) (cherry picked from commit 99dd092cd02d7af8374acf454833ce1c05fd4fd9) (cherry picked from commit 0fdbd02204d533f907cd22c83c73bf0156ec4a88) (cherry picked from commit 70b277a183c0d85966fa84e9b054f164ae2d2a44) (cherry picked from commit 3eece7fbb4e67d970d8979d0d60a58ee2a195ea5) (cherry picked from commit 4838fc9e1145a74c56926de68854234604b5e38f) (cherry picked from commit b76ed541cf4d73702a83d6b96f8618b6f8c44393) (cherry picked from commit dcdfb5b65c6fbf50798a0c49d0f879dd1285ee41) (cherry picked from commit 377dc48cdc3b1c2bcc95f86a7bf3602468ac5c39) (cherry picked from commit acc862f411c79f7832c8ba2c182af738f25f4f8b) (cherry picked from commit ac75ef101f89d58442760cec21a3f3f9199d4710) (cherry picked from commit 08f2d9f7c5b0d51358b009b0b38b626b231ec32b) (cherry picked from commit e4096f0b6441ba68719146e5a48ef44233e27a86) (cherry picked from commit bf5876f06224ac90e931f2f47b66a5b9c38b2a87) (cherry picked from commit 7dc60637e5e097b5dbc38e068ee7ba553385b496) (cherry picked from commit ef3101774ba5083e259d84db9997ff0aaddab14c) (cherry picked from commit ecb9e8867c3503387cbaf97df27d8c60a840f4a4) (cherry picked from commit 64f0ae72fec30ea443d73f8566c140682e7b9838) (cherry picked from commit 8dd6ec786294741361f79c08b0c051d2258bda02) (cherry picked from commit b36723e52b975d2e57af363db1d9118f48feade1) Conflicts: modules/context/api.go https://codeberg.org/forgejo/forgejo/pulls/1466 (cherry picked from commit 5c378e0cb823f2bad52224859ca326afb33bfd4b) (cherry picked from commit 1d87602819be9f87bf9d06203c37160568c18e78) (cherry picked from commit 0f72002d667224a75a4924ebb5557eca8bddbe70) (cherry picked from commit da2556eb13a2c976d1630315dbee8c3bc5444a11) (cherry picked from commit c01688cd900369b8cbed961f6a841ea536b07207) (cherry picked from commit af4bba832962ce4db3327c140283ce5b8d2cf6a5) (cherry picked from commit 33ca322c2ea7b05fcab084e06f8b3a6d65125808) Conflicts: modules/context/api.go https://codeberg.org/forgejo/forgejo/pulls/1739 (cherry picked from commit c18e374d4481592681ae127b723f11076c37bb91) (cherry picked from commit 27c4797c9fb3c42be252223ac0add0605f18acba) (cherry picked from commit 46588e0fea8f505de6c1a4384ae88f1cd661eea2) (cherry picked from commit b8a02ef220e3fb034d4204d5c70ffe4df72e7afd) (cherry picked from commit 05e2f49b78214d7fd48f4b31d1c9b1a7917a5e10) (cherry picked from commit e4df86d312b32f38753bf7ce1ae7953e49095d5c) (cherry picked from commit f36e06da43110c177e5e9db21a50cc96885f3e2c) (cherry picked from commit 64d336c287880a774d8eaa41193cad84e291e4b5) (cherry picked from commit 2af1cbb017f372e41482240cf0cb0af51d77e2cf) (cherry picked from commit 920741a9d667bb3b61665669932b449722883fab) (cherry picked from commit 6cbb37c8c47eb32289d0d65ad9de3cd7fff947fe) (cherry picked from commit 539cceca39f475c66d7d4fc64e10667664731f76)
2023-02-24 08:24:29 -05:00
req = NewRequestf(t, "GET", "/api/v1/user").
AddBasicAuth(user.Name)
req.Header.Set("X-Forgejo-OTP", passcode)
MakeRequest(t, req, http.StatusOK)
}
func TestAPIWebAuthn(t *testing.T) {
defer tests.PrepareTestEnv(t)()
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 32})
unittest.AssertExistsAndLoadBean(t, &auth_model.WebAuthnCredential{UserID: user.ID})
req := NewRequest(t, "GET", "/api/v1/user")
req.SetBasicAuth(user.Name, "notpassword")
resp := MakeRequest(t, req, http.StatusUnauthorized)
type userResponse struct {
Message string `json:"message"`
}
var userParsed userResponse
DecodeJSON(t, resp, &userParsed)
assert.EqualValues(t, "Basic authorization is not allowed while having security keys enrolled", userParsed.Message)
}