github.com/greenpau/go-authcrunch@v1.1.4/internal/testutils/testutils.go (about) 1 // Copyright 2022 Paul Greenberg greenpau@outlook.com 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package testutils 16 17 import ( 18 "context" 19 "fmt" 20 "github.com/greenpau/go-authcrunch/pkg/acl" 21 "github.com/greenpau/go-authcrunch/pkg/kms" 22 "github.com/greenpau/go-authcrunch/pkg/user" 23 logutil "github.com/greenpau/go-authcrunch/pkg/util/log" 24 25 "net/http" 26 "time" 27 ) 28 29 // InjectedTestToken is an instance of injected token. 30 type InjectedTestToken struct { 31 Name string `json:"name,omitempty" xml:"name,omitempty" yaml:"name,omitempty"` 32 // The locations to inject a token in this test. 33 Location string `json:"location,omitempty" xml:"location,omitempty" yaml:"location,omitempty"` 34 // The basic user claims. 35 User *user.User `json:"user,omitempty" xml:"user,omitempty" yaml:"user,omitempty"` 36 } 37 38 // NewInjectedTestToken returns an instance of injected token. 39 func NewInjectedTestToken(name, location, cfg string) *InjectedTestToken { 40 cfg = `{ 41 "exp": ` + fmt.Sprintf("%d", time.Now().Add(10*time.Minute).Unix()) + `, 42 "iat": ` + fmt.Sprintf("%d", time.Now().Add(10*time.Minute*-1).Unix()) + `, 43 "nbf": ` + fmt.Sprintf("%d", time.Date(2015, 10, 10, 12, 0, 0, 0, time.UTC).Unix()) + `, 44 ` + cfg + ` 45 "email": "smithj@outlook.com", 46 "origin": "localhost", 47 "sub": "smithj@outlook.com", 48 "roles": "anonymous guest" 49 }` 50 usr, err := user.NewUser(cfg) 51 if err != nil { 52 panic(err) 53 } 54 tkn := &InjectedTestToken{ 55 Name: name, 56 Location: location, 57 User: usr, 58 } 59 return tkn 60 } 61 62 // NewTestUser returns test User with claims. 63 func NewTestUser() *user.User { 64 cfg := `{ 65 "exp": ` + fmt.Sprintf("%d", time.Now().Add(10*time.Minute).Unix()) + `, 66 "iat": ` + fmt.Sprintf("%d", time.Now().Add(10*time.Minute*-1).Unix()) + `, 67 "nbf": ` + fmt.Sprintf("%d", time.Date(2015, 10, 10, 12, 0, 0, 0, time.UTC).Unix()) + `, 68 "name": "Smith, John", 69 "email": "smithj@outlook.com", 70 "origin": "localhost", 71 "sub": "smithj@outlook.com", 72 "roles": "anonymous guest" 73 }` 74 usr, err := user.NewUser(cfg) 75 if err != nil { 76 panic(err) 77 } 78 return usr 79 } 80 81 // NewTestGuestAccessList return ACL with guest access. 82 func NewTestGuestAccessList() *acl.AccessList { 83 ctx := context.Background() 84 rules := []*acl.RuleConfiguration{ 85 { 86 Comment: "guest access list", 87 Conditions: []string{ 88 "exact match roles anonymous guest", 89 }, 90 Action: `allow`, 91 }, 92 } 93 accessList := acl.NewAccessList() 94 if err := accessList.AddRules(ctx, rules); err != nil { 95 panic(err) 96 } 97 return accessList 98 } 99 100 // NewTestGuestAccessListWithLogger return ACL with guest access and logger. 101 func NewTestGuestAccessListWithLogger() *acl.AccessList { 102 ctx := context.Background() 103 logger := logutil.NewLogger() 104 rules := []*acl.RuleConfiguration{ 105 { 106 Comment: "guest access list", 107 Conditions: []string{ 108 "exact match roles anonymous guest", 109 }, 110 Action: `allow log`, 111 }, 112 } 113 accessList := acl.NewAccessList() 114 accessList.SetLogger(logger) 115 if err := accessList.AddRules(ctx, rules); err != nil { 116 panic(err) 117 } 118 return accessList 119 } 120 121 // NewTestCryptoKeyStore returns an instance of CryptoKeyStore with 122 // loaded HMAC key pair. 123 func NewTestCryptoKeyStore() *kms.CryptoKeyStore { 124 configs, err := kms.ParseCryptoKeyConfigs(`crypto key sign-verify ` + GetSharedKey()) 125 if err != nil { 126 panic(err) 127 } 128 keys, err := kms.GetKeysFromConfigs(configs) 129 if err != nil { 130 panic(err) 131 } 132 ks := kms.NewCryptoKeyStore() 133 if err := ks.AddKeys(keys); err != nil { 134 panic(err) 135 } 136 return ks 137 } 138 139 // GetSharedKey returns shared key for HS algorithms. 140 func GetSharedKey() string { 141 return "8b53b66e-7071-4f7c-ab9a-3ec9dd891704" 142 } 143 144 // GetCookie returns http cookie. 145 func GetCookie(name, value string, ttl int) *http.Cookie { 146 return &http.Cookie{ 147 Name: name, 148 Value: value, 149 Expires: time.Now().Add(30 * time.Duration(ttl)), 150 } 151 }