"Fossies" - the Fresh Open Source Software Archive

Member "LinOTP-release-2.11/linotpd/src/linotp/controllers/testing.py" (12 Nov 2019, 6488 Bytes) of package /linux/misc/LinOTP-release-2.11.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "testing.py" see the Fossies "Dox" file reference documentation.

    1 # -*- coding: utf-8 -*-
    2 #
    3 #    LinOTP - the open source solution for two factor authentication
    4 #    Copyright (C) 2010 - 2019 KeyIdentity GmbH
    5 #
    6 #    This file is part of LinOTP server.
    7 #
    8 #    This program is free software: you can redistribute it and/or
    9 #    modify it under the terms of the GNU Affero General Public
   10 #    License, version 3, as published by the Free Software Foundation.
   11 #
   12 #    This program is distributed in the hope that it will be useful,
   13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
   14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15 #    GNU Affero General Public License for more details.
   16 #
   17 #    You should have received a copy of the
   18 #               GNU Affero General Public License
   19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
   20 #
   21 #
   22 #    E-mail: linotp@keyidentity.com
   23 #    Contact: www.linotp.org
   24 #    Support: www.keyidentity.com
   25 #
   26 """
   27 testing controller - for testing purposes
   28 """
   29 
   30 import logging
   31 
   32 from pylons import request, response
   33 from linotp.lib.base import BaseController
   34 
   35 from linotp.lib.error import ParameterError
   36 
   37 from linotp.lib.reply import sendResult, sendError
   38 
   39 from linotp.lib.selftest import isSelfTest
   40 from linotp.lib.policy import get_auth_AutoSMSPolicy
   41 
   42 from linotp.lib.crypto import urandom
   43 
   44 import linotp.model
   45 
   46 Session = linotp.model.Session
   47 
   48 log = logging.getLogger(__name__)
   49 
   50 # from paste.debug.profile import profile_decorator
   51 
   52 # some twilio like test data
   53 twilio_ok = """<?xml version='1.0' encoding='UTF-8'?>\
   54 <TwilioResponse>\
   55 <Message>\
   56 <Sid>SM6552db38d10548cd4161826fa5754530</Sid>\
   57 <DateCreated>Mon,10 Aug 2015 08:43:33 +0000</DateCreated>\
   58 <DateUpdated>Mon, 10 Aug 2015 08:43:33+0000</DateUpdated>\
   59 <DateSent/>\
   60 <AccountSid>AC710548cd4161826fa5754530ea71fb03</AccountSid>\
   61 <To>+491171410210</To>\
   62 <From>+4911714102109</From><Body>testmessage</Body>\
   63 <Status>queued</Status><NumSegments>1</NumSegments><NumMedia>0</NumMedia>\
   64 <Direction>outbound-api</Direction><ApiVersion>2010-04-01</ApiVersion>\
   65 <Price/>\
   66 <PriceUnit>USD</PriceUnit><ErrorCode/><ErrorMessage/>\
   67 <Uri>/2010-04-01/Accounts/AC710548cd4161826fa5754530ea71fb03/Messages/SM65af\
   68 852db38d10548cd4161826fa5754</Uri>\
   69 <SubresourceUris>\
   70 <Media>/2010-04-01/Accounts/AC710548cd4161826fa5754530ea71fb03/Messages/SM65af\
   71 852db38d10548cd4161826fa5754/Media</Media>\
   72 </SubresourceUris>\
   73 </Message>\
   74 </TwilioResponse>\
   75 """
   76 twilio_fail = """<?xml version='1.0' encoding='UTF-8'?>\
   77 <TwilioResponse>\
   78 <RestException>\
   79 <Code>21603</Code>\
   80 <Message>A 'From' phone number is required.</Message>\
   81 <MoreInfo>https://www.twilio.com/docs/errors/21603</MoreInfo>\
   82 <Status>400</Status>\
   83 </RestException>\
   84 </TwilioResponse>\
   85 """
   86 
   87 
   88 class TestingController(BaseController):
   89 
   90     '''
   91     The linotp.controllers are the implementation of the web-API to talk to
   92     the LinOTP server.
   93 
   94         https://server/testing/<functionname>
   95 
   96     The functions are described below in more detail.
   97     '''
   98 
   99     def __before__(self):
  100         return response
  101 
  102     def __after__(self):
  103         return response
  104 
  105     def autosms(self):
  106         '''
  107         This function is used to test the autosms policy
  108 
  109         method:
  110             testing/autosms
  111 
  112         arguments:
  113             user    - username / loginname
  114             realm   - additional realm to match the user to a useridresolver
  115 
  116 
  117         returns:
  118             JSON response
  119         '''
  120 
  121         try:
  122             if isSelfTest() is False:
  123                 Session.rollback()
  124                 return sendError(response, "The testing controller can only"
  125                                  " be used in SelfTest mode!", 0)
  126 
  127             if "user" not in self.request_params:
  128                 raise ParameterError("Missing parameter: 'user'")
  129 
  130             ok = get_auth_AutoSMSPolicy()
  131 
  132             Session.commit()
  133             return sendResult(response, ok, 0)
  134 
  135         except Exception as exx:
  136             log.exception("[autosms] validate/check failed: %r", exx)
  137             Session.rollback()
  138             return sendError(response, ("validate/check failed: %r", exx), 0)
  139 
  140         finally:
  141             Session.close()
  142 
  143     def http2sms(self):
  144         '''
  145         This function simulates an HTTP SMS Gateway.
  146 
  147         method:
  148             test/http2sms
  149 
  150         arguments:
  151 
  152            * sender, absender
  153            * from, von
  154            * destination, ziel
  155            * password, passwort
  156            * from, von
  157            * text
  158            * account
  159            * api_id
  160 
  161 
  162         returns:
  163            As this is a test controller, the response depends on
  164            the input values.
  165 
  166             account = 5vor12, sender = legit
  167                 -> Response Success: "200" (Text)
  168 
  169             account = 5vor12, sender = <!legit>
  170                 -> Response Failed: "Failed" (Text)
  171 
  172             account = clickatel, username = legit
  173                 -> Response Success: "ID <Random Number>" (Text)
  174 
  175             account = clickatel, username = <!legit>
  176                 -> Response Success: "FAILED" (Text)
  177         '''
  178         param = self.request_params
  179 
  180         try:
  181             try:
  182                 account = param["account"]
  183             except KeyError:
  184                 raise ParameterError("Missing parameter: 'account'")
  185 
  186             sender = param.get("sender")
  187             username = param.get("username")
  188 
  189             destination = param.get("destination")
  190             if not destination:
  191                 destination = param.get("ziel")
  192 
  193             text = param.get("text")
  194 
  195             if not destination:
  196                 raise Exception("Missing <destination>")
  197 
  198             if not text:
  199                 raise Exception("Missing <text>")
  200 
  201             if account == "5vor12":
  202                 if sender == "legit":
  203                     return "200"
  204                 else:
  205                     return "Failed"
  206 
  207             elif account == "clickatel":
  208                 if username == "legit":
  209                     return "ID %i" % int(urandom.randint(1000))
  210                 else:
  211                     return "FAILED"
  212 
  213             elif account == "twilio":
  214                 if username == "legit":
  215                     return twilio_ok
  216                 else:
  217                     return twilio_fail
  218 
  219             Session.commit()
  220             return "Missing account info."
  221 
  222         except Exception as e:
  223             log.exception('[http2sms] %r' % e)
  224             Session.rollback()
  225             return sendError(response, unicode(e), 0)
  226 
  227         finally:
  228             Session.close()
  229 
  230 
  231 # eof #