Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Commit

Permalink
Remove copyright, trim empty lines
Browse files Browse the repository at this point in the history
  • Loading branch information
APwhitehat committed May 22, 2018
1 parent 73912fb commit bb5863e
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 15 deletions.
13 changes: 3 additions & 10 deletions src/github.com/matrix-org/dendrite/clientapi/auth/tokens/tokens.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// Copyright 2018 New Vector 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
Expand Down Expand Up @@ -46,13 +44,11 @@ type TokenOptions struct {
// GenerateLoginToken generates a short term login token to be used as
// token authentication ("m.login.token")
func GenerateLoginToken(op TokenOptions) (string, error) {

if !isValidTokenOptions(op) {
return "", errors.New("The given TokenOptions is invalid")
}

mac, err := generateBaseMacaroon(op.ServerPrivateKey, op.ServerName, op.UserID)

if err != nil {
return "", err
}
Expand All @@ -63,12 +59,11 @@ func GenerateLoginToken(op TokenOptions) (string, error) {
now := time.Now().Second()
expiryCaveat := TimePrefix + strconv.Itoa(now+op.Duration)
err = mac.AddFirstPartyCaveat([]byte(expiryCaveat))

if err != nil {
return "", macaroonError(err)
}
urlSafeEncode, err := SerializeMacaroon(*mac)

urlSafeEncode, err := SerializeMacaroon(*mac)
if err != nil {
return "", macaroonError(err)
}
Expand Down Expand Up @@ -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.
func SerializeMacaroon(m macaroon.Macaroon) (string, error) {
bin, err := m.MarshalBinary()

if err != nil {
return "", err
}
urlSafeEncode := base64.RawURLEncoding.EncodeToString(bin)

urlSafeEncode := base64.RawURLEncoding.EncodeToString(bin)
return urlSafeEncode, nil
}

Expand All @@ -129,11 +123,10 @@ func SerializeMacaroon(m macaroon.Macaroon) (string, error) {
func DeSerializeMacaroon(urlSafeEncode string) (macaroon.Macaroon, error) {
var mac macaroon.Macaroon
bin, err := base64.RawURLEncoding.DecodeString(urlSafeEncode)

if err != nil {
return mac, err
}
err = mac.UnmarshalBinary(bin)

err = mac.UnmarshalBinary(bin)
return mac, err
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,13 @@ var (
func TestGenerateLoginToken(t *testing.T) {
// Test valid
_, err := GenerateLoginToken(validTokenOp)

if err != nil {
t.Errorf("Token generation failed for valid TokenOptions with err: %s", err.Error())
}

// Test invalids
for missing, invalidTokenOp := range invalidTokenOps {
_, err := GenerateLoginToken(invalidTokenOp)

if err == nil {
t.Errorf("Token generation should fail for TokenOptions with missing %s", missing)
}
Expand All @@ -62,19 +60,16 @@ func serializationTestError(err error) string {

func TestSerialization(t *testing.T) {
fakeToken, err := GenerateLoginToken(validTokenOp)

if err != nil {
t.Errorf(serializationTestError(err))
}

fakeMacaroon, err := DeSerializeMacaroon(fakeToken)

if err != nil {
t.Errorf(serializationTestError(err))
}

sameFakeToken, err := SerializeMacaroon(fakeMacaroon)

if err != nil {
t.Errorf(serializationTestError(err))
}
Expand Down

0 comments on commit bb5863e

Please sign in to comment.