summaryrefslogtreecommitdiff
path: root/lib/metamask.hoon
blob: 3456692c6885886c569e7f2166e54d5fba85f04e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/+  naive, ethereum, server, sr=sortug
=>
|%
+$  challenges  (set secret)
+$  secret  @uv
+$  authorization
  $:  who=@p
      =secret
      adr=tape
      sig=tape
  ==
:: +$  user-sessions  (map coki=@ [proven=@p src=@p])
+$  user-sessions  (map coki=@ ship=@p)
+$  sessions
  $:  =challenges
      users=user-sessions
  ==
--
|_  [=sessions =bowl:gall]
+$  sess  ^sessions
::  state field to keep track of users logged with metamask
  ::  this goes on the router
  ++  serve-metamask-challenge  
    |=  eyre-id=@ta
    ::  special-case MetaMask auth handling
    =/  new-challenge  (sham [now eny]:bowl)
    %+  weld  (self-poke [%meta new-challenge])
    %+  give-simple-payload:app:server
      eyre-id
    ^-  simple-payload:http
    :-  :-  200
        ~[['Content-Type' 'application/json']]
    `(as-octs:mimes:html (en:json:html (enjs-challenge new-challenge)))
  ::  Modified from ~rabsef-bicrym's %mask by ~hanfel-dovned.
  ++  process-metamask-auth
    |=  [order-id=@t octs=(unit octs)]
    ^-  (list card:agent:gall)
    =/  challenges  challenges.sessions
    |^
    ?~  octs  ~|(%empty-auth-request !!)
    :: ?.  =('auth' (cut 3 [0 4] q.u.octs))
    ::   *(list card:agent:gall)
    =/  jon  (de:json:html q.u.octs)
    ?~  jon  ~|(%empty-auth-json !!)
    =/  body=json  u.jon
    =/  axn  (dejs-action body)
    =/  is-valid  (validate who.axn secret.axn adr.axn sig.axn)
    ~&  >>  signature-valid=[is-valid who.axn secret.axn adr.axn sig.axn]
    ?.  is-valid  ~|(%bad-metamask-signature !!)
    
    =/  coki-hash  session-hash
    =/  coki  (session-cookie-string coki-hash who.axn)
    %+  weld
    (self-poke [%auth who.axn coki-hash secret.axn])
    %+  give-simple-payload:app:server
      order-id
    ^-  simple-payload:http
    :-  :-  303
      :~
        ['set-cookie' coki]
        ['location' '/zodiac']
      ==
    =/  obj=json  %-  pairs:enjs:format  :~([%login-ok [%b .y]])
    `(as-octs:mimes:html (en:json:html obj))
    
    ++  validate
      |=  [who=@p challenge=secret address=tape hancock=tape]
      ^-  ?
      =/  addy  (from-tape address)
      =/  cock  (from-tape hancock)
      =/  owner  (get-owner who)  ?~  owner  
        ~&  "no owner"  
        %.n
      ?.  =(addy u.owner)  
        ~&  "wrong owner"  
        %.n
      ?.  (~(has in challenges) challenge)  
        ~&  "bad challenge"  
        %.n
      =/  note=@uvI
        =+  octs=(as-octs:mimes:html (scot %uv challenge))
        %-  keccak-256:keccak:crypto
        %-  as-octs:mimes:html
        ;:  (cury cat 3)
          '\19Ethereum Signed Message:\0a'
          (crip (a-co:co p.octs))
          q.octs
        ==
      ?.  &(=(20 (met 3 addy)) =(65 (met 3 cock)))  
        ~&  "addy != cock"  
        %.n
      =/  r  (cut 3 [33 32] cock)
      =/  s  (cut 3 [1 32] cock)
      =/  v=@
        =+  v=(cut 3 [0 1] cock)
        ?+  v  99
          %0   0
          %1   1
          %27  0
          %28  1
        ==
      ?.  |(=(0 v) =(1 v))  
        ~&  "wrong v"
        %.n
      =/  xy
        (ecdsa-raw-recover:secp256k1:secp:crypto note v r s)
      =/  pub  :((cury cat 3) y.xy x.xy 0x4)
      =/  add  (address-from-pub:key:ethereum pub)
      =(addy add)
    ::
    ++  from-tape
      |=(h=tape ^-(@ux (scan h ;~(pfix (jest '0x') hex))))
    ::
    ++  get-owner
      |=  who=@p
      ^-  (unit @ux)
      =-  ?~  pin=`(unit point:naive)`-
            ~
          ?.  |(?=(%l1 dominion.u.pin) ?=(%l2 dominion.u.pin))
            ~
          `address.owner.own.u.pin
      .^  (unit point:naive)
        %gx
        %+  en-beam
          [our.bowl %azimuth [%da now.bowl]]
        /point/(scot %p who)/noun
      ==
    ++  dejs-action
      |=  jon=json
      ^-  authorization
      =,  dejs:format
      %.  jon
      %-  ot
      :: :~  [%who (se %p)]
      :~  [%who ni]
          [%secret (se %uv)]
          [%address sa]
          [%signature sa]
      ==
    --
  ++  enjs-challenge
    =,  enjs:format
    |=  chal=@
    ^-  json
    %-  pairs
    :~  [%challenge [%s (scot %uv chal)]]
    ==
  ++  self-poke
    |=  noun=*
    ^-  (list card:agent:gall)
    :~  [%pass /gib %agent [our.bowl dap.bowl] %poke %noun !>(noun)]
    ==

  ::  these are the poke handlers
  ++  handle-meta
    |=  new-challenge=@  ^-  ^sessions
    :: =?    users.sessions
    ::     !(~(has by users.sessions) src.bowl)
    ::   (~(put by users.sessions) [src.bowl src.bowl])
    :: =?    challenges.sessions
    ::     =(src.bowl (~(got by users.sessions) src.bowl))
      =.  challenges.sessions
      (~(put in challenges.sessions) new-challenge)

    sessions
  ++  handle-auth
    |=  [who=@p coki=@ =secret]  ^-  ^sessions
    :: ~&  >  "%ustj: Successful authentication of {<src>} as {<who>}."
    =.  users.sessions        (~(put by users.sessions) coki who)
    =.  challenges.sessions   (~(del in challenges.sessions) secret)
    sessions
  
  ++  session-timeout
    |%
    ++  auth   ~d30
    ++  guest  ~d7
    --
  ++  session-hash
    (~(raw og (shas %coki eny.bowl)) 128)
  ++  session-cookie-string  |=  [session=@ proven=@p]
    ^-  @t
    =/  max-age=tape  %-  a-co:co
      =/  its-a-me  .=(src.bowl our.bowl)
      =,  session-timeout
      (div (msec:milly ?:(its-a-me auth guest)) 1.000)
    %-  crip
      "urbneo-{(scow %p proven)}={(scow:parsing:sr %uv session)}; Path=/; Max-Age={max-age}"
      :: "urbneo={(scow:parsing:sr %uv session)}; Path=/; Max-Age={max-age}"
  ++  validate-coki  |=  coki=@t  ^-  (unit @p)
    ~&  >>  validating-coki=coki
    =/  cookies  (rush coki cock:de-purl:html)
    ~&  cookies=cookies
    ?~  cookies  ~
    =/  cokis=(list [@t @t])  u.cookies
    |-
      ?~  cokis  ~
      =/  hd  i.cokis
      :: ?:  (contains:string:sr (trip hd) "urbneo")
        ~&  key=-.hd
        =/  hash  (slaw:parsing:sr %uv +.hd)
        ?~  hash  $(cokis t.cokis)
        ~&  hash=`@uv`u.hash
        =/  sess  (~(get by users.sessions) u.hash)
        ~&  sess=sess
        ?~  sess  $(cokis t.cokis)
        `u.sess
  ++  parse-coki  |=  coki=@t
    =/  cookies  (rush coki cock:de-purl:html)
    ?~  cookies  users.sessions
    =/  cokis=(list [@t @t])  u.cookies
    |-
      ?~  cokis  users.sessions
      =/  hd  i.cokis
      :: ?:  (contains:string:sr (trip hd) "urbneo")
      ~&  key=-.hd
      =/  hash  (slaw:parsing:sr %uv +.hd)
      ?~  hash  $(cokis t.cokis)
      =/  sess  (~(get by users.sessions) u.hash)
      ?~  sess  $(cokis t.cokis)
      (~(del by users.sessions) u.hash)

  ++  logout  |=  [order-id=@t coki=@t]
    (self-poke [%logout order-id coki])
  ++  handle-logout  |=  [order-id=@t coki=@t]
    ~&  handling-logout=coki
    =/  new-users  (parse-coki coki)
    :_  sessions(users new-users)
    %+  give-simple-payload:app:server
      order-id
    (redirect:gen:server '/zodiac/login')
  --