mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-12 17:33:09 -06:00
Remove copyright, trim empty lines
This commit is contained in:
parent
73912fbf75
commit
bb5863e15e
|
|
@ -1,5 +1,3 @@
|
||||||
// Copyright 2018 New Vector Ltd
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
// you may not use this file except in compliance with the License.
|
// you may not use this file except in compliance with the License.
|
||||||
// You may obtain a copy of the License at
|
// You may obtain a copy of the License at
|
||||||
|
|
@ -46,13 +44,11 @@ type TokenOptions struct {
|
||||||
// GenerateLoginToken generates a short term login token to be used as
|
// GenerateLoginToken generates a short term login token to be used as
|
||||||
// token authentication ("m.login.token")
|
// token authentication ("m.login.token")
|
||||||
func GenerateLoginToken(op TokenOptions) (string, error) {
|
func GenerateLoginToken(op TokenOptions) (string, error) {
|
||||||
|
|
||||||
if !isValidTokenOptions(op) {
|
if !isValidTokenOptions(op) {
|
||||||
return "", errors.New("The given TokenOptions is invalid")
|
return "", errors.New("The given TokenOptions is invalid")
|
||||||
}
|
}
|
||||||
|
|
||||||
mac, err := generateBaseMacaroon(op.ServerPrivateKey, op.ServerName, op.UserID)
|
mac, err := generateBaseMacaroon(op.ServerPrivateKey, op.ServerName, op.UserID)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
@ -63,12 +59,11 @@ func GenerateLoginToken(op TokenOptions) (string, error) {
|
||||||
now := time.Now().Second()
|
now := time.Now().Second()
|
||||||
expiryCaveat := TimePrefix + strconv.Itoa(now+op.Duration)
|
expiryCaveat := TimePrefix + strconv.Itoa(now+op.Duration)
|
||||||
err = mac.AddFirstPartyCaveat([]byte(expiryCaveat))
|
err = mac.AddFirstPartyCaveat([]byte(expiryCaveat))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", macaroonError(err)
|
return "", macaroonError(err)
|
||||||
}
|
}
|
||||||
urlSafeEncode, err := SerializeMacaroon(*mac)
|
|
||||||
|
|
||||||
|
urlSafeEncode, err := SerializeMacaroon(*mac)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", macaroonError(err)
|
return "", macaroonError(err)
|
||||||
}
|
}
|
||||||
|
|
@ -115,12 +110,11 @@ func macaroonError(err error) error {
|
||||||
// returns it's base64 encoded string, URL safe, which can be sent via web, email, etc.
|
// returns it's base64 encoded string, URL safe, which can be sent via web, email, etc.
|
||||||
func SerializeMacaroon(m macaroon.Macaroon) (string, error) {
|
func SerializeMacaroon(m macaroon.Macaroon) (string, error) {
|
||||||
bin, err := m.MarshalBinary()
|
bin, err := m.MarshalBinary()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
urlSafeEncode := base64.RawURLEncoding.EncodeToString(bin)
|
|
||||||
|
|
||||||
|
urlSafeEncode := base64.RawURLEncoding.EncodeToString(bin)
|
||||||
return urlSafeEncode, nil
|
return urlSafeEncode, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -129,11 +123,10 @@ func SerializeMacaroon(m macaroon.Macaroon) (string, error) {
|
||||||
func DeSerializeMacaroon(urlSafeEncode string) (macaroon.Macaroon, error) {
|
func DeSerializeMacaroon(urlSafeEncode string) (macaroon.Macaroon, error) {
|
||||||
var mac macaroon.Macaroon
|
var mac macaroon.Macaroon
|
||||||
bin, err := base64.RawURLEncoding.DecodeString(urlSafeEncode)
|
bin, err := base64.RawURLEncoding.DecodeString(urlSafeEncode)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return mac, err
|
return mac, err
|
||||||
}
|
}
|
||||||
err = mac.UnmarshalBinary(bin)
|
|
||||||
|
|
||||||
|
err = mac.UnmarshalBinary(bin)
|
||||||
return mac, err
|
return mac, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,6 @@ var (
|
||||||
func TestGenerateLoginToken(t *testing.T) {
|
func TestGenerateLoginToken(t *testing.T) {
|
||||||
// Test valid
|
// Test valid
|
||||||
_, err := GenerateLoginToken(validTokenOp)
|
_, err := GenerateLoginToken(validTokenOp)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Token generation failed for valid TokenOptions with err: %s", err.Error())
|
t.Errorf("Token generation failed for valid TokenOptions with err: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
@ -49,7 +48,6 @@ func TestGenerateLoginToken(t *testing.T) {
|
||||||
// Test invalids
|
// Test invalids
|
||||||
for missing, invalidTokenOp := range invalidTokenOps {
|
for missing, invalidTokenOp := range invalidTokenOps {
|
||||||
_, err := GenerateLoginToken(invalidTokenOp)
|
_, err := GenerateLoginToken(invalidTokenOp)
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("Token generation should fail for TokenOptions with missing %s", missing)
|
t.Errorf("Token generation should fail for TokenOptions with missing %s", missing)
|
||||||
}
|
}
|
||||||
|
|
@ -62,19 +60,16 @@ func serializationTestError(err error) string {
|
||||||
|
|
||||||
func TestSerialization(t *testing.T) {
|
func TestSerialization(t *testing.T) {
|
||||||
fakeToken, err := GenerateLoginToken(validTokenOp)
|
fakeToken, err := GenerateLoginToken(validTokenOp)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf(serializationTestError(err))
|
t.Errorf(serializationTestError(err))
|
||||||
}
|
}
|
||||||
|
|
||||||
fakeMacaroon, err := DeSerializeMacaroon(fakeToken)
|
fakeMacaroon, err := DeSerializeMacaroon(fakeToken)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf(serializationTestError(err))
|
t.Errorf(serializationTestError(err))
|
||||||
}
|
}
|
||||||
|
|
||||||
sameFakeToken, err := SerializeMacaroon(fakeMacaroon)
|
sameFakeToken, err := SerializeMacaroon(fakeMacaroon)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf(serializationTestError(err))
|
t.Errorf(serializationTestError(err))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue