|
| 1 | +/******** Peter Winzell (c), 11/27/23 *********************************************/ |
| 2 | + |
| 3 | +package atServer |
| 4 | + |
| 5 | +import ( |
| 6 | + "bytes" |
| 7 | + "crypto/rsa" |
| 8 | + "encoding/json" |
| 9 | + "github.com/w3c/automotive-viss2/utils" |
| 10 | + "log" |
| 11 | + _ "log" |
| 12 | + "net/http" |
| 13 | + "strconv" |
| 14 | + "testing" |
| 15 | +) |
| 16 | + |
| 17 | +//Prequisites: AGT Server and Vissv2 Server should be up and running (0.0.0.0 in docker) |
| 18 | +type TResponse struct { |
| 19 | + Token string `json:"token"` |
| 20 | +} |
| 21 | + |
| 22 | +const agt_posttesturl = "http://0.0.0.0:7500/agts" |
| 23 | +const at_url = "http://0.0.0.0:8600/ats" |
| 24 | +const SHORT_TERM_TOKEN_LENGTH = 14400 |
| 25 | +const LONG_TERM_TOKEN_LENGTH = 345600 |
| 26 | + |
| 27 | +func getShortTermAGTResponse() (*http.Response, error) { |
| 28 | + body := []byte(`{"vin":"GEO001","context":"Independent+OEM+Cloud","proof":"ABC","key":"DEF"}`) |
| 29 | + |
| 30 | + r, err := http.NewRequest("POST", agt_posttesturl, bytes.NewBuffer(body)) |
| 31 | + if err != nil { |
| 32 | + panic(err) |
| 33 | + } |
| 34 | + r.Header.Add("Content-Type", "application/json") |
| 35 | + client := &http.Client{} |
| 36 | + res, err := client.Do(r) |
| 37 | + if err != nil { |
| 38 | + panic(err) |
| 39 | + } |
| 40 | + return res, err |
| 41 | +} |
| 42 | + |
| 43 | +func getLongTermAGTResponse() (*http.Response, error) { |
| 44 | + var privKey *rsa.PrivateKey |
| 45 | + err := utils.ImportRsaKey(PRIV_KEY_DIRECTORY, &privKey) |
| 46 | + if err != nil { |
| 47 | + return nil, err |
| 48 | + } |
| 49 | + |
| 50 | + popToken := utils.PopToken{} |
| 51 | + token, err := popToken.GenerateToken(privKey) // generate a proof-of-possession-token to get a LT token. |
| 52 | + |
| 53 | + agtP := agtPayload{ |
| 54 | + Vin: "GEO001", |
| 55 | + Context: "Independent+OEM+Cloud", |
| 56 | + Proof: "ABC", |
| 57 | + Key: popToken.Jwk.Thumb, // Thumb print need to match. |
| 58 | + } |
| 59 | + |
| 60 | + body, err := json.Marshal(agtP) // POST a acces grant token request |
| 61 | + |
| 62 | + r, err := http.NewRequest("POST", agt_posttesturl, bytes.NewBuffer(body)) |
| 63 | + if err != nil { |
| 64 | + return nil, err |
| 65 | + } |
| 66 | + r.Header.Add("Content-Type", "application/json") |
| 67 | + r.Header.Add("PoP", token) // add the Pop as a proof of possesion. |
| 68 | + |
| 69 | + client := &http.Client{} |
| 70 | + |
| 71 | + return client.Do(r) |
| 72 | +} |
| 73 | + |
| 74 | +func TestShortTermTokenAccess(t *testing.T) { |
| 75 | + |
| 76 | + res, err := getShortTermAGTResponse() |
| 77 | + if err != nil { |
| 78 | + t.Error(err) |
| 79 | + } |
| 80 | + |
| 81 | + defer res.Body.Close() |
| 82 | + |
| 83 | + post := &TResponse{} |
| 84 | + derr := json.NewDecoder(res.Body).Decode(post) |
| 85 | + if derr != nil { |
| 86 | + panic(derr) |
| 87 | + } |
| 88 | + |
| 89 | + if res.StatusCode != http.StatusCreated { |
| 90 | + t.Error("status code expected to be 201 , got: ", res.StatusCode) |
| 91 | + } |
| 92 | + |
| 93 | + log.Printf("got token = %s", post.Token) |
| 94 | + var Agt utils.ExtendedJwt |
| 95 | + err = Agt.DecodeFromFull(post.Token) // parsing the JWT token |
| 96 | + vin := Agt.PayloadClaims["vin"] |
| 97 | + ctx := Agt.PayloadClaims["clx"] |
| 98 | + iat, err := strconv.Atoi(Agt.PayloadClaims["iat"]) |
| 99 | + exp, err := strconv.Atoi(Agt.PayloadClaims["exp"]) |
| 100 | + |
| 101 | + //test that it is correct length |
| 102 | + if (exp - iat) != SHORT_TERM_TOKEN_LENGTH { |
| 103 | + t.Error("short term token error: ", exp-iat) |
| 104 | + } |
| 105 | + //test vin and context |
| 106 | + if vin != "GEO001" { |
| 107 | + t.Error("Vin does not match => ", vin) |
| 108 | + } |
| 109 | + if ctx != "Independent+OEM+Cloud" { |
| 110 | + t.Error("roles fails to match => ", ctx) |
| 111 | + } |
| 112 | + |
| 113 | +} |
| 114 | + |
| 115 | +const PRIV_KEY_DIRECTORY = "../../agt_server/agt_private_key.rsa" |
| 116 | + |
| 117 | +//TODO this should reside in utils somwhere , duplicate code. |
| 118 | +type agtPayload struct { |
| 119 | + Vin string `json:"vin"` |
| 120 | + Context string `json:"context"` |
| 121 | + Proof string `json:"proof"` |
| 122 | + //Key utils.JsonWebKey `json:"key"` |
| 123 | + Key string `json:"key"` |
| 124 | +} |
| 125 | + |
| 126 | +func TestLongTermTokenAccess(t *testing.T) { |
| 127 | + |
| 128 | + res, err := getLongTermAGTResponse() |
| 129 | + if err != nil { |
| 130 | + panic(err) |
| 131 | + } |
| 132 | + |
| 133 | + defer res.Body.Close() |
| 134 | + |
| 135 | + post := &TResponse{} |
| 136 | + derr := json.NewDecoder(res.Body).Decode(post) |
| 137 | + if derr != nil { |
| 138 | + panic(derr) |
| 139 | + } |
| 140 | + |
| 141 | + if res.StatusCode != http.StatusCreated { |
| 142 | + t.Error("status code expected to be 201 , got: ", res.StatusCode) |
| 143 | + } |
| 144 | + |
| 145 | + log.Printf("got token = %s", post.Token) |
| 146 | + var Agt utils.ExtendedJwt |
| 147 | + err = Agt.DecodeFromFull(post.Token) // parsing the JWT token |
| 148 | + vin := Agt.PayloadClaims["vin"] |
| 149 | + ctx := Agt.PayloadClaims["clx"] |
| 150 | + iat, err := strconv.Atoi(Agt.PayloadClaims["iat"]) |
| 151 | + exp, err := strconv.Atoi(Agt.PayloadClaims["exp"]) |
| 152 | + |
| 153 | + //test that it is correct length |
| 154 | + if (exp - iat) != LONG_TERM_TOKEN_LENGTH { |
| 155 | + t.Error("short term token error: ", exp-iat) |
| 156 | + } |
| 157 | + //test vin and context |
| 158 | + if vin != "GEO001" { |
| 159 | + t.Error("Vin does not match => ", vin) |
| 160 | + } |
| 161 | + if ctx != "Independent+OEM+Cloud" { |
| 162 | + t.Error("roles fails to match => ", ctx) |
| 163 | + } |
| 164 | + |
| 165 | +} |
| 166 | + |
| 167 | +type atRequest struct { |
| 168 | + Token string `json:string "token"` |
| 169 | + Purpose string `json:string "purpose"` |
| 170 | + Pop string `json:string "pop"` |
| 171 | +} |
| 172 | + |
| 173 | +func getAtToken(agttoken string) (*http.Response, error) { |
| 174 | + |
| 175 | + atReq := &atRequest{ |
| 176 | + Token: agttoken, |
| 177 | + Purpose: "fuel-status", |
| 178 | + Pop: "GHI", |
| 179 | + } |
| 180 | + body, err := json.Marshal(atReq) |
| 181 | + if err != nil { |
| 182 | + return nil, err |
| 183 | + } |
| 184 | + |
| 185 | + r, err := http.NewRequest("POST", at_url, bytes.NewBuffer(body)) |
| 186 | + if err != nil { |
| 187 | + return nil, err |
| 188 | + } |
| 189 | + |
| 190 | + r.Header.Add("Content-Type", "application/json") |
| 191 | + client := &http.Client{} |
| 192 | + res, err := client.Do(r) |
| 193 | + if err != nil { |
| 194 | + return nil, err |
| 195 | + } |
| 196 | + return res, err |
| 197 | +} |
| 198 | + |
| 199 | +func parseATToken(res http.Response, t *testing.T) *TResponse { |
| 200 | + |
| 201 | + defer res.Body.Close() |
| 202 | + |
| 203 | + post := &TResponse{} |
| 204 | + derr := json.NewDecoder(res.Body).Decode(post) |
| 205 | + if derr != nil { |
| 206 | + t.Error("could not parse http response") |
| 207 | + return nil |
| 208 | + } |
| 209 | + |
| 210 | + if res.StatusCode != http.StatusCreated { |
| 211 | + t.Error("status code expected to be 201 , got: ", res.StatusCode) |
| 212 | + |
| 213 | + } |
| 214 | + |
| 215 | + return post |
| 216 | +} |
| 217 | + |
| 218 | +// Viss server must be up and running |
| 219 | +func TestAtTokenAccess_ST(t *testing.T) { |
| 220 | + res_ag, err := getShortTermAGTResponse() // Get Access Grant Token |
| 221 | + if err != nil { |
| 222 | + t.Error(err) |
| 223 | + } |
| 224 | + |
| 225 | + res, err := getAtToken(parseATToken(*res_ag, t).Token) // Get Access token |
| 226 | + |
| 227 | + if err != nil { |
| 228 | + t.Error(err) |
| 229 | + } |
| 230 | + if res.StatusCode != http.StatusCreated { |
| 231 | + t.Error("status code expected to be 201 , got: ", res.StatusCode) |
| 232 | + } else { |
| 233 | + attokenpost := &TResponse{} |
| 234 | + derr := json.NewDecoder(res.Body).Decode(attokenpost) |
| 235 | + |
| 236 | + if derr != nil { |
| 237 | + t.Error(derr) |
| 238 | + } |
| 239 | + log.Println(attokenpost.Token) |
| 240 | + } |
| 241 | + |
| 242 | +} |
| 243 | + |
| 244 | +func TestAtTokenAccess_LT(t *testing.T) { |
| 245 | + t.Error("not implemented yet") |
| 246 | + |
| 247 | +} |
| 248 | + |
| 249 | +// Test actual requests against server, server and a feeder for south-bound must be running. |
| 250 | + |
| 251 | +func TestGetAccessControlST(t *testing.T) { |
| 252 | + t.Error("not implemented yet") |
| 253 | +} |
| 254 | + |
| 255 | +func TestGetAccessControlLT(t *testing.T) { |
| 256 | + t.Error("not implemented yet") |
| 257 | +} |
0 commit comments