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
167 lines
4.5 KiB
Go
167 lines
4.5 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package mfa
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/base32"
|
|
"fmt"
|
|
"net/url"
|
|
"strings"
|
|
|
|
"github.com/dgryski/dgoogauth"
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
"github.com/mattermost/rsc/qr"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
// InvalidToken indicates the case where the token validation has failed.
|
|
var InvalidToken = errors.New("invalid mfa token")
|
|
|
|
const (
|
|
// This will result in 160 bits of entropy (base32 encoded), as recommended by rfc4226.
|
|
mfaSecretSize = 20
|
|
)
|
|
|
|
type Store interface {
|
|
UpdateMfaActive(userId string, active bool) error
|
|
UpdateMfaSecret(userId, secret string) error
|
|
StoreMfaUsedTimestamps(userId string, ts []int) error
|
|
GetMfaUsedTimestamps(userId string) ([]int, error)
|
|
}
|
|
|
|
type MFA struct {
|
|
store Store
|
|
}
|
|
|
|
func New(store Store) *MFA {
|
|
return &MFA{store}
|
|
}
|
|
|
|
// newRandomBase32String returns a base32 encoded string of a random slice
|
|
// of bytes of the given size. The resulting entropy will be (8 * size) bits.
|
|
func newRandomBase32String(size int) string {
|
|
data := make([]byte, size)
|
|
rand.Read(data)
|
|
return base32.StdEncoding.EncodeToString(data)
|
|
}
|
|
|
|
func getIssuerFromURL(uri string) string {
|
|
issuer := "Mattermost"
|
|
siteURL := strings.TrimSpace(uri)
|
|
|
|
if siteURL != "" {
|
|
siteURL = strings.TrimPrefix(siteURL, "https://")
|
|
siteURL = strings.TrimPrefix(siteURL, "http://")
|
|
issuer = strings.TrimPrefix(siteURL, "www.")
|
|
}
|
|
|
|
return url.QueryEscape(issuer)
|
|
}
|
|
|
|
// GenerateSecret generates a new user mfa secret and store it with the StoreSecret function provided
|
|
func (m *MFA) GenerateSecret(siteURL, userEmail, userID string) (string, []byte, error) {
|
|
issuer := getIssuerFromURL(siteURL)
|
|
|
|
secret := newRandomBase32String(mfaSecretSize)
|
|
|
|
authLink := fmt.Sprintf("otpauth://totp/%s:%s?secret=%s&issuer=%s", issuer, userEmail, secret, issuer)
|
|
|
|
code, err := qr.Encode(authLink, qr.H)
|
|
|
|
if err != nil {
|
|
return "", nil, errors.Wrap(err, "unable to generate qr code")
|
|
}
|
|
|
|
img := code.PNG()
|
|
|
|
if err := m.store.UpdateMfaSecret(userID, secret); err != nil {
|
|
return "", nil, errors.Wrap(err, "unable to store mfa secret")
|
|
}
|
|
|
|
return secret, img, nil
|
|
}
|
|
|
|
// Activate set the mfa as active and store it with the StoreActive function provided
|
|
func (m *MFA) Activate(userMfaSecret, userID string, token string) error {
|
|
usedTs, err := m.store.GetMfaUsedTimestamps(userID)
|
|
if err != nil {
|
|
return errors.Wrap(err, "unable to retrieve the DisallowReuse slice")
|
|
}
|
|
|
|
otpConfig, err := m.authenticate(userMfaSecret, usedTs, token)
|
|
if err != nil {
|
|
return errors.Wrap(err, "unable to authenticate the token")
|
|
}
|
|
|
|
if err = m.store.UpdateMfaActive(userID, true); err != nil {
|
|
return errors.Wrap(err, "unable to store mfa active")
|
|
}
|
|
|
|
err = m.store.StoreMfaUsedTimestamps(userID, otpConfig.DisallowReuse)
|
|
if err != nil {
|
|
return errors.Wrap(err, "unable to store the DisallowReuse slice")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Deactivate set the mfa as deactivated, remove the mfa secret, store it with the StoreActive and StoreSecret functions provided
|
|
func (m *MFA) Deactivate(userId string) error {
|
|
if err := m.store.UpdateMfaActive(userId, false); err != nil {
|
|
return errors.Wrap(err, "unable to store mfa active")
|
|
}
|
|
|
|
if err := m.store.UpdateMfaSecret(userId, ""); err != nil {
|
|
return errors.Wrap(err, "unable to store mfa secret")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Validate the provide token using the secret provided
|
|
func (m *MFA) ValidateToken(user *model.User, token string) (bool, error) {
|
|
usedTs, err := m.store.GetMfaUsedTimestamps(user.Id)
|
|
if err != nil {
|
|
return false, errors.Wrap(err, "unable to retrieve the DisallowReuse slice")
|
|
}
|
|
|
|
otpConfig, err := m.authenticate(user.MfaSecret, usedTs, token)
|
|
if err != nil {
|
|
if err == InvalidToken {
|
|
return false, nil
|
|
}
|
|
|
|
return false, errors.Wrap(err, "unable to parse the token")
|
|
}
|
|
|
|
err = m.store.StoreMfaUsedTimestamps(user.Id, otpConfig.DisallowReuse)
|
|
if err != nil {
|
|
return true, errors.Wrap(err, "unable to store the DisallowReuse slice")
|
|
}
|
|
|
|
return true, nil
|
|
}
|
|
|
|
func (*MFA) authenticate(userMfaSecret string, usedTs []int, token string) (*dgoogauth.OTPConfig, error) {
|
|
trimmedToken := strings.TrimSpace(token)
|
|
|
|
otpConfig := &dgoogauth.OTPConfig{
|
|
Secret: userMfaSecret,
|
|
WindowSize: 3,
|
|
HotpCounter: 0,
|
|
DisallowReuse: usedTs,
|
|
}
|
|
|
|
ok, err := otpConfig.Authenticate(trimmedToken)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "unable to parse the token")
|
|
}
|
|
if !ok {
|
|
return nil, InvalidToken
|
|
}
|
|
|
|
return otpConfig, nil
|
|
}
|