From de4469d148156f2357bf0ba99c04d87b46955b0a Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Fri, 26 Oct 2018 18:27:58 +0200 Subject: [PATCH] Get first otp checks running --- nameko1.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 2 deletions(-) diff --git a/nameko1.py b/nameko1.py index 2c67142..983acd0 100644 --- a/nameko1.py +++ b/nameko1.py @@ -4,7 +4,9 @@ from nameko.rpc import rpc import json from nameko.web.handlers import http from nameko.timer import timer +from nameko.rpc import rpc, RpcProxy +import pyotp class ServiceA: @@ -45,10 +47,9 @@ class HttpService: class ServiceTimer: name ="servicetimer" - dispatch = EventDispatcher() - @timer(interval=1) + @timer(interval=3) def ping(self): # method executed every second print("pong") @@ -61,3 +62,58 @@ class LoggerService: @event_handler("servicetimer", "ping") def handle_event(self, payload): print("timing receive in logger: ", payload) + + +class OTPClient: + name = "generic-service-using-otp" + + totp = pyotp.TOTP("JBSWY3DPEHPK3PXP") + + otp = RpcProxy("otp") + + @timer(interval=3) + def auth(self): + token = self.totp.now() + print("Verifying using {}".format(token)) + print("Auth1: {}".format(self.otp.verify("app1", token))) + print("Auth-wrongapp: {}".format(self.otp.verify("app2", token))) + print("Auth-noapp: {}".format(self.otp.verify("appNOAPP", token))) + +class OTPSeed: + name = "generic-service-using-otp-seed" + + otp = RpcProxy("otp") + + @timer(interval=10) + def auth(self): + seed = otp.get_seed("app1") + totp = pyotp.TOTP(seed) + + print("seed / token: {} {}".format(seed, token)) + +class OTPService: + name = "otp" + + otp_tokens = { + 'app1': 'JBSWY3DPEHPK3PXP', + 'app2': 'AIEIU3IAAA' + } + + @rpc + def get_seed(self, appid): + if appid in self.otp_tokens: + return self.otp_tokens[appid] + else: + return "NO SEED" + + @rpc + def verify(self, appid, token): + if not appid in self.otp_tokens: + return "NO SUCH APP {}".format(appid) + + totp = pyotp.TOTP(self.otp_tokens[appid]) + + if totp.verify(token, valid_window=3): + return "OK" + else: + return "FAIL"