Files
upstream-mattermost/server/channels/store/storetest/oauth_store.go
upstream-mirror 240db4e2a7
Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
Server CI Nightly Race / Build Environment (push) Failing after 1s
snapshot of https://github.com/mattermost/mattermost.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 00:29:55 +05:00

399 lines
13 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package storetest
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/shared/request"
"github.com/mattermost/mattermost/server/v8/channels/store"
)
func TestOAuthStore(t *testing.T, rctx request.CTX, ss store.Store) {
t.Run("SaveApp", func(t *testing.T) { testOAuthStoreSaveApp(t, rctx, ss) })
t.Run("GetApp", func(t *testing.T) { testOAuthStoreGetApp(t, rctx, ss) })
t.Run("UpdateApp", func(t *testing.T) { testOAuthStoreUpdateApp(t, rctx, ss) })
t.Run("SaveAccessData", func(t *testing.T) { testOAuthStoreSaveAccessData(t, rctx, ss) })
t.Run("OAuthUpdateAccessData", func(t *testing.T) { testOAuthUpdateAccessData(t, rctx, ss) })
t.Run("GetAccessData", func(t *testing.T) { testOAuthStoreGetAccessData(t, rctx, ss) })
t.Run("RemoveAccessData", func(t *testing.T) { testOAuthStoreRemoveAccessData(t, rctx, ss) })
t.Run("RemoveAllAccessData", func(t *testing.T) { testOAuthStoreRemoveAllAccessData(t, rctx, ss) })
t.Run("SaveAuthData", func(t *testing.T) { testOAuthStoreSaveAuthData(t, rctx, ss) })
t.Run("GetAuthData", func(t *testing.T) { testOAuthStoreGetAuthData(t, rctx, ss) })
t.Run("RemoveAuthData", func(t *testing.T) { testOAuthStoreRemoveAuthData(t, rctx, ss) })
t.Run("RemoveAuthDataByUser", func(t *testing.T) { testOAuthStoreRemoveAuthDataByUser(t, rctx, ss) })
t.Run("OAuthGetAuthorizedApps", func(t *testing.T) { testOAuthGetAuthorizedApps(t, rctx, ss) })
t.Run("OAuthGetAccessDataByUserForApp", func(t *testing.T) { testOAuthGetAccessDataByUserForApp(t, rctx, ss) })
t.Run("DeleteApp", func(t *testing.T) { testOAuthStoreDeleteApp(t, rctx, ss) })
}
func testOAuthStoreSaveApp(t *testing.T, rctx request.CTX, ss store.Store) {
a1 := model.OAuthApp{}
a1.CreatorId = model.NewId()
a1.CallbackUrls = []string{"https://nowhere.com"}
a1.Homepage = "https://nowhere.com"
// Try to save an app that already has an Id
a1.Id = model.NewId()
_, err := ss.OAuth().SaveApp(&a1)
require.Error(t, err, "Should have failed, cannot add an OAuth app cannot be save with an Id, it has to be updated")
// Try to save an Invalid App
a1.Id = ""
_, err = ss.OAuth().SaveApp(&a1)
require.Error(t, err, "Should have failed, app should be invalid cause it doesn' have a name set")
a1.Name = "TestApp" + model.NewId() // Valid name
a1.MattermostAppID = "a very, very, very, very, very, very, very long id"
_, err = ss.OAuth().SaveApp(&a1)
require.Error(t, err, "Should have failed, app should be invalid cause the MattermostAppID is to long")
// Save the app
a1.Id = ""
a1.MattermostAppID = "some small id" // Valid id
_, err = ss.OAuth().SaveApp(&a1)
require.NoError(t, err)
}
func testOAuthStoreGetApp(t *testing.T, rctx request.CTX, ss store.Store) {
a1 := model.OAuthApp{}
a1.CreatorId = model.NewId()
a1.Name = "TestApp" + model.NewId()
a1.CallbackUrls = []string{"https://nowhere.com"}
a1.Homepage = "https://nowhere.com"
_, err := ss.OAuth().SaveApp(&a1)
require.NoError(t, err)
// Lets try to get and app that does not exists
_, err = ss.OAuth().GetApp("fake0123456789abcderfgret1")
require.Error(t, err, "Should have failed. App does not exists")
_, err = ss.OAuth().GetApp(a1.Id)
require.NoError(t, err)
// Lets try and get the app from a user that hasn't created any apps
apps, err := ss.OAuth().GetAppByUser("fake0123456789abcderfgret1", 0, 1000)
require.NoError(t, err)
assert.Empty(t, apps, "Should have failed. Fake user hasn't created any apps")
_, err = ss.OAuth().GetAppByUser(a1.CreatorId, 0, 1000)
require.NoError(t, err)
_, err = ss.OAuth().GetApps(0, 1000)
require.NoError(t, err)
}
func testOAuthStoreUpdateApp(t *testing.T, rctx request.CTX, ss store.Store) {
a1 := model.OAuthApp{}
a1.CreatorId = model.NewId()
a1.Name = "TestApp" + model.NewId()
a1.CallbackUrls = []string{"https://nowhere.com"}
a1.Homepage = "https://nowhere.com"
_, err := ss.OAuth().SaveApp(&a1)
require.NoError(t, err)
// temporarily save the created app id
id := a1.Id
a1.CreateAt = 1
a1.ClientSecret = "pwd"
a1.CreatorId = "12345678901234567890123456"
// Lets update the app by removing the name
a1.Name = ""
_, err = ss.OAuth().UpdateApp(&a1)
require.Error(t, err, "Should have failed. App name is not set")
// Lets not find the app that we are trying to update
a1.Id = "fake0123456789abcderfgret1"
a1.Name = "NewName"
_, err = ss.OAuth().UpdateApp(&a1)
require.Error(t, err, "Should have failed. Not able to find the app")
a1.Id = id
ua, err := ss.OAuth().UpdateApp(&a1)
require.NoError(t, err)
require.Equal(t, ua.Name, "NewName", "name did not update")
require.NotEqual(t, ua.CreateAt, 1, "create at should not have updated")
require.NotEqual(t, ua.CreatorId, "12345678901234567890123456", "creator id should not have updated")
}
func testOAuthStoreSaveAccessData(t *testing.T, rctx request.CTX, ss store.Store) {
a1 := model.AccessData{}
a1.ClientId = model.NewId()
a1.UserId = model.NewId()
// Lets try and save an incomplete access data
_, err := ss.OAuth().SaveAccessData(&a1)
require.Error(t, err, "Should have failed. Access data needs the token")
a1.Token = model.NewId()
a1.RefreshToken = model.NewId()
a1.RedirectUri = "http://example.com"
_, err = ss.OAuth().SaveAccessData(&a1)
require.NoError(t, err)
}
func testOAuthUpdateAccessData(t *testing.T, rctx request.CTX, ss store.Store) {
a1 := model.AccessData{}
a1.ClientId = model.NewId()
a1.UserId = model.NewId()
a1.Token = model.NewId()
a1.RefreshToken = model.NewId()
a1.ExpiresAt = model.GetMillis()
a1.RedirectUri = "http://example.com"
_, err := ss.OAuth().SaveAccessData(&a1)
require.NoError(t, err)
//Try to update to invalid Refresh Token
refreshToken := a1.RefreshToken
a1.RefreshToken = model.NewId() + "123"
_, err = ss.OAuth().UpdateAccessData(&a1)
require.Error(t, err, "Should have failed with invalid token")
//Try to update to invalid RedirectUri
a1.RefreshToken = model.NewId()
a1.RedirectUri = ""
_, err = ss.OAuth().UpdateAccessData(&a1)
require.Error(t, err, "Should have failed with invalid Redirect URI")
// Should update fine
a1.RedirectUri = "http://example.com"
ra1, err := ss.OAuth().UpdateAccessData(&a1)
require.NoError(t, err)
require.NotEqual(t, ra1.RefreshToken, refreshToken, "refresh tokens didn't match")
}
func testOAuthStoreGetAccessData(t *testing.T, rctx request.CTX, ss store.Store) {
a1 := model.AccessData{}
a1.ClientId = model.NewId()
a1.UserId = model.NewId()
a1.Token = model.NewId()
a1.RefreshToken = model.NewId()
a1.ExpiresAt = model.GetMillis()
a1.RedirectUri = "http://example.com"
_, err := ss.OAuth().SaveAccessData(&a1)
require.NoError(t, err)
_, err = ss.OAuth().GetAccessData("invalidToken")
require.Error(t, err, "Should have failed. There is no data with an invalid token")
ra1, err := ss.OAuth().GetAccessData(a1.Token)
require.NoError(t, err)
assert.Equal(t, a1.Token, ra1.Token, "tokens didn't match")
_, err = ss.OAuth().GetPreviousAccessData(a1.UserId, a1.ClientId)
require.NoError(t, err)
_, err = ss.OAuth().GetPreviousAccessData("user", "junk")
require.NoError(t, err)
// Try to get the Access data using an invalid refresh token
_, err = ss.OAuth().GetAccessDataByRefreshToken(a1.Token)
require.Error(t, err, "Should have failed. There is no data with an invalid token")
// Get the Access Data using the refresh token
ra1, err = ss.OAuth().GetAccessDataByRefreshToken(a1.RefreshToken)
require.NoError(t, err)
assert.Equal(t, a1.RefreshToken, ra1.RefreshToken, "tokens didn't match")
}
func testOAuthStoreRemoveAccessData(t *testing.T, rctx request.CTX, ss store.Store) {
a1 := model.AccessData{}
a1.ClientId = model.NewId()
a1.UserId = model.NewId()
a1.Token = model.NewId()
a1.RefreshToken = model.NewId()
a1.RedirectUri = "http://example.com"
_, err := ss.OAuth().SaveAccessData(&a1)
require.NoError(t, err)
err = ss.OAuth().RemoveAccessData(a1.Token)
require.NoError(t, err)
result, _ := ss.OAuth().GetPreviousAccessData(a1.UserId, a1.ClientId)
require.Nil(t, result, "did not delete access token")
}
func testOAuthStoreRemoveAllAccessData(t *testing.T, rctx request.CTX, ss store.Store) {
a1 := model.AccessData{}
a1.ClientId = model.NewId()
a1.UserId = model.NewId()
a1.Token = model.NewId()
a1.RefreshToken = model.NewId()
a1.RedirectUri = "http://example.com"
_, err := ss.OAuth().SaveAccessData(&a1)
require.NoError(t, err)
err = ss.OAuth().RemoveAllAccessData()
require.NoError(t, err)
result, _ := ss.OAuth().GetPreviousAccessData(a1.UserId, a1.ClientId)
require.Nil(t, result, "did not delete access token")
}
func testOAuthStoreSaveAuthData(t *testing.T, rctx request.CTX, ss store.Store) {
a1 := model.AuthData{}
a1.ClientId = model.NewId()
a1.UserId = model.NewId()
a1.Code = model.NewId()
a1.RedirectUri = "http://example.com"
_, err := ss.OAuth().SaveAuthData(&a1)
require.NoError(t, err)
}
func testOAuthStoreGetAuthData(t *testing.T, rctx request.CTX, ss store.Store) {
a1 := model.AuthData{}
a1.ClientId = model.NewId()
a1.UserId = model.NewId()
a1.Code = model.NewId()
a1.RedirectUri = "http://example.com"
_, err := ss.OAuth().SaveAuthData(&a1)
require.NoError(t, err)
_, err = ss.OAuth().GetAuthData(a1.Code)
require.NoError(t, err)
}
func testOAuthStoreRemoveAuthData(t *testing.T, rctx request.CTX, ss store.Store) {
a1 := model.AuthData{}
a1.ClientId = model.NewId()
a1.UserId = model.NewId()
a1.Code = model.NewId()
a1.RedirectUri = "http://example.com"
_, err := ss.OAuth().SaveAuthData(&a1)
require.NoError(t, err)
err = ss.OAuth().RemoveAuthData(a1.Code)
require.NoError(t, err)
_, err = ss.OAuth().GetAuthData(a1.Code)
require.Error(t, err, "should have errored - auth code removed")
}
func testOAuthStoreRemoveAuthDataByUser(t *testing.T, rctx request.CTX, ss store.Store) {
a1 := model.AuthData{}
a1.ClientId = model.NewId()
a1.UserId = model.NewId()
a1.Code = model.NewId()
a1.RedirectUri = "http://example.com"
_, err := ss.OAuth().SaveAuthData(&a1)
require.NoError(t, err)
err = ss.OAuth().PermanentDeleteAuthDataByUser(a1.UserId)
require.NoError(t, err)
}
func testOAuthGetAuthorizedApps(t *testing.T, rctx request.CTX, ss store.Store) {
a1 := model.OAuthApp{}
a1.CreatorId = model.NewId()
a1.Name = "TestApp" + model.NewId()
a1.CallbackUrls = []string{"https://nowhere.com"}
a1.Homepage = "https://nowhere.com"
_, err := ss.OAuth().SaveApp(&a1)
require.NoError(t, err)
// Lets try and get an Authorized app for a user who hasn't authorized it
apps, err := ss.OAuth().GetAuthorizedApps("fake0123456789abcderfgret1", 0, 1000)
require.NoError(t, err)
assert.Empty(t, apps, "Should have failed. Fake user hasn't authorized the app")
// allow the app
p := model.Preference{}
p.UserId = a1.CreatorId
p.Category = model.PreferenceCategoryAuthorizedOAuthApp
p.Name = a1.Id
p.Value = "true"
nErr := ss.Preference().Save(model.Preferences{p})
require.NoError(t, nErr)
apps, err = ss.OAuth().GetAuthorizedApps(a1.CreatorId, 0, 1000)
require.NoError(t, err)
assert.NotEqual(t, len(apps), 0, "It should have return apps")
}
func testOAuthGetAccessDataByUserForApp(t *testing.T, rctx request.CTX, ss store.Store) {
a1 := model.OAuthApp{}
a1.CreatorId = model.NewId()
a1.Name = "TestApp" + model.NewId()
a1.CallbackUrls = []string{"https://nowhere.com"}
a1.Homepage = "https://nowhere.com"
_, err := ss.OAuth().SaveApp(&a1)
require.NoError(t, err)
// allow the app
p := model.Preference{}
p.UserId = a1.CreatorId
p.Category = model.PreferenceCategoryAuthorizedOAuthApp
p.Name = a1.Id
p.Value = "true"
nErr := ss.Preference().Save(model.Preferences{p})
require.NoError(t, nErr)
apps, err := ss.OAuth().GetAuthorizedApps(a1.CreatorId, 0, 1000)
require.NoError(t, err)
assert.NotEqual(t, len(apps), 0, "It should have return apps")
// save the token
ad1 := model.AccessData{}
ad1.ClientId = a1.Id
ad1.UserId = a1.CreatorId
ad1.Token = model.NewId()
ad1.RefreshToken = model.NewId()
ad1.RedirectUri = "http://example.com"
_, err = ss.OAuth().SaveAccessData(&ad1)
require.NoError(t, err)
accessData, err := ss.OAuth().GetAccessDataByUserForApp(a1.CreatorId, a1.Id)
require.NoError(t, err)
assert.NotEqual(t, len(accessData), 0, "It should have return access data")
}
func testOAuthStoreDeleteApp(t *testing.T, rctx request.CTX, ss store.Store) {
a1 := model.OAuthApp{}
a1.CreatorId = model.NewId()
a1.Name = "TestApp" + model.NewId()
a1.CallbackUrls = []string{"https://nowhere.com"}
a1.Homepage = "https://nowhere.com"
_, err := ss.OAuth().SaveApp(&a1)
require.NoError(t, err)
// delete a non-existent app
err = ss.OAuth().DeleteApp("fakeclientId")
require.NoError(t, err)
s1 := &model.Session{}
s1.UserId = model.NewId()
s1.Token = model.NewId()
s1.IsOAuth = true
s1, nErr := ss.Session().Save(rctx, s1)
require.NoError(t, nErr)
ad1 := model.AccessData{}
ad1.ClientId = a1.Id
ad1.UserId = a1.CreatorId
ad1.Token = s1.Token
ad1.RefreshToken = model.NewId()
ad1.RedirectUri = "http://example.com"
_, err = ss.OAuth().SaveAccessData(&ad1)
require.NoError(t, err)
err = ss.OAuth().DeleteApp(a1.Id)
require.NoError(t, err)
_, nErr = ss.Session().Get(rctx, s1.Token)
require.Error(t, nErr, "should error - session should be deleted")
_, err = ss.OAuth().GetAccessData(s1.Token)
require.Error(t, err, "should error - access data should be deleted")
}