2017-04-20 23:40:52 +01:00
|
|
|
// 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.
|
|
|
|
|
2017-05-23 17:43:05 +01:00
|
|
|
// Package auth implements authentication checks and storage.
|
2017-03-07 13:43:32 +00:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
2017-09-18 15:51:26 +01:00
|
|
|
"context"
|
2017-05-30 17:51:40 +01:00
|
|
|
"crypto/rand"
|
2017-05-25 13:33:50 +01:00
|
|
|
"database/sql"
|
2017-05-30 17:51:40 +01:00
|
|
|
"encoding/base64"
|
2017-03-07 13:43:32 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2017-05-23 17:43:05 +01:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
2017-03-07 13:43:32 +00:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
|
|
|
"github.com/matrix-org/util"
|
|
|
|
)
|
|
|
|
|
2017-05-30 17:51:40 +01:00
|
|
|
// OWASP recommends at least 128 bits of entropy for tokens: https://www.owasp.org/index.php/Insufficient_Session-ID_Length
|
|
|
|
// 32 bytes => 256 bits
|
|
|
|
var tokenByteLength = 32
|
|
|
|
|
2017-10-10 10:40:52 +01:00
|
|
|
// The length of generated device IDs
|
|
|
|
var deviceIDByteLength = 6
|
|
|
|
|
2017-07-17 17:20:57 +01:00
|
|
|
// DeviceDatabase represents a device database.
|
|
|
|
type DeviceDatabase interface {
|
2017-08-23 15:08:48 +01:00
|
|
|
// Look up the device matching the given access token.
|
2017-09-18 15:51:26 +01:00
|
|
|
GetDeviceByAccessToken(ctx context.Context, token string) (*authtypes.Device, error)
|
2017-07-17 17:20:57 +01:00
|
|
|
}
|
|
|
|
|
2017-03-07 13:43:32 +00:00
|
|
|
// VerifyAccessToken verifies that an access token was supplied in the given HTTP request
|
2017-05-23 17:43:05 +01:00
|
|
|
// and returns the device it corresponds to. Returns resErr (an error response which can be
|
2017-03-07 13:43:32 +00:00
|
|
|
// sent to the client) if the token is invalid or there was a problem querying the database.
|
2017-07-17 17:20:57 +01:00
|
|
|
func VerifyAccessToken(req *http.Request, deviceDB DeviceDatabase) (device *authtypes.Device, resErr *util.JSONResponse) {
|
2017-05-23 17:43:05 +01:00
|
|
|
token, err := extractAccessToken(req)
|
|
|
|
if err != nil {
|
2017-03-07 13:43:32 +00:00
|
|
|
resErr = &util.JSONResponse{
|
|
|
|
Code: 401,
|
2017-05-23 17:43:05 +01:00
|
|
|
JSON: jsonerror.MissingToken(err.Error()),
|
2017-03-07 13:43:32 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2017-09-18 15:51:26 +01:00
|
|
|
device, err = deviceDB.GetDeviceByAccessToken(req.Context(), token)
|
2017-05-23 17:43:05 +01:00
|
|
|
if err != nil {
|
2017-05-25 13:33:50 +01:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
resErr = &util.JSONResponse{
|
2017-10-10 10:40:52 +01:00
|
|
|
Code: 401,
|
|
|
|
JSON: jsonerror.UnknownToken("Unknown token"),
|
2017-05-25 13:33:50 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
resErr = &util.JSONResponse{
|
|
|
|
Code: 500,
|
|
|
|
JSON: jsonerror.Unknown("Failed to check access token"),
|
|
|
|
}
|
2017-05-23 17:43:05 +01:00
|
|
|
}
|
2017-03-07 13:43:32 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-05-30 17:51:40 +01:00
|
|
|
// GenerateAccessToken creates a new access token. Returns an error if failed to generate
|
|
|
|
// random bytes.
|
|
|
|
func GenerateAccessToken() (string, error) {
|
|
|
|
b := make([]byte, tokenByteLength)
|
|
|
|
if _, err := rand.Read(b); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
// url-safe no padding
|
|
|
|
return base64.RawURLEncoding.EncodeToString(b), nil
|
|
|
|
}
|
|
|
|
|
2017-10-10 10:40:52 +01:00
|
|
|
// GenerateDeviceID creates a new device id. Returns an error if failed to generate
|
|
|
|
// random bytes.
|
|
|
|
func GenerateDeviceID() (string, error) {
|
|
|
|
b := make([]byte, deviceIDByteLength)
|
|
|
|
_, err := rand.Read(b)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
// url-safe no padding
|
|
|
|
return base64.RawURLEncoding.EncodeToString(b), nil
|
|
|
|
}
|
|
|
|
|
2017-03-07 13:43:32 +00:00
|
|
|
// extractAccessToken from a request, or return an error detailing what went wrong. The
|
|
|
|
// error message MUST be human-readable and comprehensible to the client.
|
|
|
|
func extractAccessToken(req *http.Request) (string, error) {
|
|
|
|
// cf https://github.com/matrix-org/synapse/blob/v0.19.2/synapse/api/auth.py#L631
|
|
|
|
authBearer := req.Header.Get("Authorization")
|
|
|
|
queryToken := req.URL.Query().Get("access_token")
|
|
|
|
if authBearer != "" && queryToken != "" {
|
|
|
|
return "", fmt.Errorf("mixing Authorization headers and access_token query parameters")
|
|
|
|
}
|
|
|
|
|
|
|
|
if queryToken != "" {
|
|
|
|
return queryToken, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if authBearer != "" {
|
|
|
|
parts := strings.SplitN(authBearer, " ", 2)
|
|
|
|
if len(parts) != 2 || parts[0] != "Bearer" {
|
|
|
|
return "", fmt.Errorf("invalid Authorization header")
|
|
|
|
}
|
|
|
|
return parts[1], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", fmt.Errorf("missing access token")
|
|
|
|
}
|