storage.go 5.0 KB
Newer Older
K
Kegsay 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright 2017 Vector Creations Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package devices

17
import (
18
	"context"
19 20
	"database/sql"

21
	"github.com/matrix-org/dendrite/clientapi/auth"
22
	"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
23
	"github.com/matrix-org/dendrite/common"
24
	"github.com/matrix-org/gomatrixserverlib"
25 26
)

K
Kegsay 已提交
27 28
// Database represents a device database.
type Database struct {
29 30
	db      *sql.DB
	devices devicesStatements
K
Kegsay 已提交
31 32 33
}

// NewDatabase creates a new device database
34 35 36 37 38 39 40 41 42 43 44
func NewDatabase(dataSourceName string, serverName gomatrixserverlib.ServerName) (*Database, error) {
	var db *sql.DB
	var err error
	if db, err = sql.Open("postgres", dataSourceName); err != nil {
		return nil, err
	}
	d := devicesStatements{}
	if err = d.prepare(db, serverName); err != nil {
		return nil, err
	}
	return &Database{db, d}, nil
45 46 47
}

// GetDeviceByAccessToken returns the device matching the given access token.
48
// Returns sql.ErrNoRows if no matching device was found.
49 50 51 52
func (d *Database) GetDeviceByAccessToken(
	ctx context.Context, token string,
) (*authtypes.Device, error) {
	return d.devices.selectDeviceByToken(ctx, token)
K
Kegsay 已提交
53
}
54

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
// GetDeviceByID returns the device matching the given ID.
// Returns sql.ErrNoRows if no matching device was found.
func (d *Database) GetDeviceByID(
	ctx context.Context, localpart, deviceID string,
) (*authtypes.Device, error) {
	return d.devices.selectDeviceByID(ctx, localpart, deviceID)
}

// GetDevicesByLocalpart returns the devices matching the given localpart.
func (d *Database) GetDevicesByLocalpart(
	ctx context.Context, localpart string,
) ([]authtypes.Device, error) {
	return d.devices.selectDevicesByLocalpart(ctx, localpart)
}

70 71
// CreateDevice makes a new device associated with the given user ID localpart.
// If there is already a device with the same device ID for this user, that access token will be revoked
72 73
// and replaced with the given accessToken. If the given accessToken is already in use for another device,
// an error will be returned.
74
// If no device ID is given one is generated.
75
// Returns the device on success.
76
func (d *Database) CreateDevice(
77
	ctx context.Context, localpart string, deviceID *string, accessToken string,
P
Paul Tötterman 已提交
78
	displayName *string,
79
) (dev *authtypes.Device, returnErr error) {
80 81 82 83 84 85 86 87
	if deviceID != nil {
		returnErr = common.WithTransaction(d.db, func(txn *sql.Tx) error {
			var err error
			// Revoke existing token for this device
			if err = d.devices.deleteDevice(ctx, txn, *deviceID, localpart); err != nil {
				return err
			}

P
Paul Tötterman 已提交
88
			dev, err = d.devices.insertDevice(ctx, txn, *deviceID, localpart, accessToken, displayName)
89
			return err
90 91 92 93 94 95 96 97 98 99
		})
	} else {
		// We generate device IDs in a loop in case its already taken.
		// We cap this at going round 5 times to ensure we don't spin forever
		var newDeviceID string
		for i := 1; i <= 5; i++ {
			newDeviceID, returnErr = auth.GenerateDeviceID()
			if returnErr != nil {
				return
			}
100

101 102
			returnErr = common.WithTransaction(d.db, func(txn *sql.Tx) error {
				var err error
P
Paul Tötterman 已提交
103
				dev, err = d.devices.insertDevice(ctx, txn, newDeviceID, localpart, accessToken, displayName)
104 105 106 107 108 109 110
				return err
			})
			if returnErr == nil {
				return
			}
		}
	}
111 112 113
	return
}

P
Paul Tötterman 已提交
114 115 116 117 118 119 120 121 122 123
// UpdateDevice updates the given device with the display name.
// Returns SQL error if there are problems and nil on success.
func (d *Database) UpdateDevice(
	ctx context.Context, localpart, deviceID string, displayName *string,
) error {
	return common.WithTransaction(d.db, func(txn *sql.Tx) error {
		return d.devices.updateDeviceName(ctx, txn, localpart, deviceID, displayName)
	})
}

B
Brendan Abolivier 已提交
124 125 126 127
// RemoveDevice revokes a device by deleting the entry in the database
// matching with the given device ID and user ID localpart
// If the device doesn't exist, it will not return an error
// If something went wrong during the deletion, it will return the SQL error
128 129 130
func (d *Database) RemoveDevice(
	ctx context.Context, deviceID, localpart string,
) error {
131
	return common.WithTransaction(d.db, func(txn *sql.Tx) error {
132
		if err := d.devices.deleteDevice(ctx, txn, deviceID, localpart); err != sql.ErrNoRows {
B
Brendan Abolivier 已提交
133 134 135 136 137
			return err
		}
		return nil
	})
}
R
Remi Reuvekamp 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150 151

// RemoveAllDevices revokes devices by deleting the entry in the
// database matching the given user ID localpart.
// If something went wrong during the deletion, it will return the SQL error.
func (d *Database) RemoveAllDevices(
	ctx context.Context, localpart string,
) error {
	return common.WithTransaction(d.db, func(txn *sql.Tx) error {
		if err := d.devices.deleteDevicesByLocalpart(ctx, txn, localpart); err != sql.ErrNoRows {
			return err
		}
		return nil
	})
}