permissions.mx raw

   1  package main
   2  
   3  import (
   4  	"smesh.lol/web/common/helpers"
   5  	"smesh.lol/web/common/jsbridge/ext"
   6  )
   7  
   8  // Per-host, per-method permission store.
   9  // Policies: "allow", "deny", "ask" (default).
  10  
  11  type permission struct {
  12  	Host   string
  13  	Method string
  14  	Policy string
  15  }
  16  
  17  var permissions []permission
  18  
  19  func init() {
  20  	ext.StorageGet("smesh-permissions", func(data string) {
  21  		if data != "" {
  22  			parsePermissions(data)
  23  		}
  24  	})
  25  }
  26  
  27  func getPermission(host, method string) string {
  28  	for _, p := range permissions {
  29  		if p.Host == host && p.Method == method {
  30  			return p.Policy
  31  		}
  32  	}
  33  	return "ask"
  34  }
  35  
  36  func setPermission(host, method, policy string) {
  37  	for i, p := range permissions {
  38  		if p.Host == host && p.Method == method {
  39  			permissions[i].Policy = policy
  40  			savePermissions()
  41  			return
  42  		}
  43  	}
  44  	permissions = append(permissions, permission{Host: host, Method: method, Policy: policy})
  45  	savePermissions()
  46  }
  47  
  48  func getPermissionsJSON() string {
  49  	s := "["
  50  	for i, p := range permissions {
  51  		if i > 0 {
  52  			s += ","
  53  		}
  54  		s += "{\"host\":" + helpers.JsonString(p.Host) +
  55  			",\"method\":" + helpers.JsonString(p.Method) +
  56  			",\"policy\":" + helpers.JsonString(p.Policy) + "}"
  57  	}
  58  	return "{\"result\":" + s + "]}"
  59  }
  60  
  61  func savePermissions() {
  62  	s := "["
  63  	for i, p := range permissions {
  64  		if i > 0 {
  65  			s += ","
  66  		}
  67  		s += "{\"host\":" + helpers.JsonString(p.Host) +
  68  			",\"method\":" + helpers.JsonString(p.Method) +
  69  			",\"policy\":" + helpers.JsonString(p.Policy) + "}"
  70  	}
  71  	ext.StorageSet("smesh-permissions", s+"]")
  72  }
  73  
  74  func parsePermissions(s string) {
  75  	permissions = nil
  76  	i := 0
  77  	for i < len(s) && s[i] != '[' {
  78  		i++
  79  	}
  80  	i++
  81  	for i < len(s) {
  82  		for i < len(s) && s[i] != '{' && s[i] != ']' {
  83  			i++
  84  		}
  85  		if i >= len(s) || s[i] == ']' {
  86  			break
  87  		}
  88  		end := i + 1
  89  		depth := 1
  90  		for end < len(s) && depth > 0 {
  91  			if s[end] == '{' {
  92  				depth++
  93  			} else if s[end] == '}' {
  94  				depth--
  95  			} else if s[end] == '"' {
  96  				end++
  97  				for end < len(s) && s[end] != '"' {
  98  					if s[end] == '\\' {
  99  						end++
 100  					}
 101  					end++
 102  				}
 103  			}
 104  			end++
 105  		}
 106  		obj := s[i:end]
 107  		host := helpers.JsonGetString(obj, "host")
 108  		method := helpers.JsonGetString(obj, "method")
 109  		policy := helpers.JsonGetString(obj, "policy")
 110  		if host != "" && method != "" && policy != "" {
 111  			permissions = append(permissions, permission{Host: host, Method: method, Policy: policy})
 112  		}
 113  		i = end
 114  	}
 115  }
 116