# Maintaining a Flag Between Response and Request (\<- yep)

**URL:** <https://discourse.mitmproxy.org/t/maintaining-a-flag-between-response-and-request-yep/652>\
**Category:** help\
**Created:** [October 11, 2017, 5:05pm UTC](https://discourse.mitmproxy.org/t/maintaining-a-flag-between-response-and-request-yep/652 "2017-10-11T17:05:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bede](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@bede](https://discourse.mitmproxy.org/u/bede)\
**Post date:** [October 11, 2017, 5:05pm UTC](https://discourse.mitmproxy.org/t/maintaining-a-flag-between-response-and-request-yep/652/1 "2017-10-11T17:05:27Z")

</div>

Hi there! I’m using mitmdump with a custom script to ensure that a my requests always have a valid (custom) authentication header in the request. I’m using mitmdump as an upstream proxy in conjunction with Burp’s scanner so the requests that come through are automated. Essentially, I need maintain a couple of global variables between each request/response to indicate whether or not a particular header value is still valid. If a given response indicates that the session token is invalid, I would want to update a global flag to indicate that I need a new auth token. My next request would need to inspect that flag and perform the appropriate steps to grab a new auth token if needed, save it to another global var and reset the global “is valid” flag. That globally saved auth token would need to subsequently added/updated to the outbound request.

So in essence:

```
global vars: 
  isValidSess, authToken

response code:
  if response body/status/whatever indicates an invalid session then set isValidSess = false

request code
  if isValidSess == false, then 
     perform side task to get a new token
     save new auth token to authToken
     set isValidSess == true

  update outbound headers so that authToken is added to the request

```

What would be the best method to maintain session between the separate req/resp pairs? I’m saying “global variable”, but it’s only to get the idea across of what I’m looking to do…and I’m a bit new to python. 🙂

---

<div class="post-metadata">

**Author:** ![mhils](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mitmproxy.org/mhils/32/7_2.png) [@mhils](https://discourse.mitmproxy.org/u/mhils)\
**Post date:** [October 16, 2017, 2:01am UTC](https://discourse.mitmproxy.org/t/maintaining-a-flag-between-response-and-request-yep/652/2 "2017-10-16T02:01:24Z")

</div>

Hi @bede,

Take a look at for example [https://github.com/mitmproxy/mitmproxy/blob/master/mitmproxy/addons/proxyauth.py#L52](https://github.com/mitmproxy/mitmproxy/blob/master/mitmproxy/addons/proxyauth.py#L52) - using an Addon class and defining a mapping for [flow.id](http://flow.id) in there should work fine. If you only need to persist information for a single flow, you can use the `flow.metadata` dict.
