func (s *Server) isAllowedByPolicy(p *peer.Peer, req *msg.OcReq) (bool, msg.OcRespStatus) { fmt.Printf("is allowed? %v\n", s) paidPv, err := p.AmountPaid(SERVER_PAYMENT_MIN_CONF, s.BtcConf) if err != nil { return false, msg.SERVER_ERROR } consumedPv, err := p.AmountConsumed() if err != nil { return false, msg.SERVER_ERROR } fmt.Printf("paid: %v, consumed: %v\n", paidPv, consumedPv) // policies := s.Conf.MatchingPolicies(req.Service, req.Method) // for _, policy := range policies { // fmt.Printf("check against policy: %v\n", policy) // switch policy.Cmd { // case conf.ALLOW: // continue // case conf.DENY: // return false, msg.ACCESS_DENIED // case conf.MIN_FEE: // min := policy.Args[0].(msg.PaymentValue) // fmt.Printf("min fee: %v, pt: %v\n", min, req.PaymentType) // if req.PaymentType != msg.ATTACHED { // return false, msg.PAYMENT_REQUIRED // } // // TODO(ortutay): implement // return false, msg.SERVER_ERROR // } // } return true, msg.OK }
func (s *Server) checkBalance(p *peer.Peer) *msg.OcResp { balance, err := p.Balance(SERVER_PAYMENT_MIN_CONF, s.BtcConf) if err != nil { return msg.NewRespError(msg.SERVER_ERROR) } fmt.Printf("balance: %v\n", balance) if balance.Currency != msg.BTC { panic("TODO: support other currencies") } maxBalance, err := s.Conf.PolicyForCmd(conf.MAX_BALANCE) if err != nil { // TODO(ortutay): handle more configuration around max balance panic(err) } maxAllowed := maxBalance.Args[0].(*msg.PaymentValue).Amount fmt.Printf("max balance: %v\n", maxBalance.Args[0]) if balance.Amount > maxAllowed { addr, err := p.PaymentAddr(-1, s.BtcConf) if err != nil { return msg.NewRespError(msg.SERVER_ERROR) } // TODO(ortutay): a clever client will notice that they can pay off just a // small amount to stay just at the edge of the max balance. they do not // much by doing this, and may waste money on miner fees, but nevertheless, // we could have some smarter handling for that situation. pr := msg.PaymentRequest{ Amount: balance.Amount, Currency: balance.Currency, Addr: addr, } body, err := json.Marshal(&pr) if err != nil { return msg.NewRespError(msg.SERVER_ERROR) } return msg.NewRespErrorWithBody(msg.PLEASE_PAY, []byte(body)) } return nil }