File size: 3,634 Bytes
5e3a6e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import time
import os
from typing import Dict
import streamlit as st
from hydralit import HydraHeadApp


class SignUpApp(HydraHeadApp):
    """

    This is an example signup application to be used to secure access within a HydraApp streamlit application.



    This application is an example of allowing an application to run from the login without requiring authentication.

    

    """

    def __init__(self, title = '', **kwargs):
        self.__dict__.update(kwargs)
        self.title = title


    def run(self) -> None:
        """

        Application entry point.



        """

        st.markdown("<h1 style='text-align: center;'>Secure Hydralit Signup</h1>", unsafe_allow_html=True)

        c1,c2,c3 = st.columns([2,2,2])
        c3.image("./resources/lock.png",width=100,)
        c3.image("./resources/hydra.png",width=100,)

        pretty_btn = """

        <style>

        div[class="row-widget stButton"] > button {

            width: 100%;

        }

        </style>

        <br><br>

        """
        c2.markdown(pretty_btn,unsafe_allow_html=True)
        
        if 'MSG' in os.environ.keys():
            st.info(os.environ['MSG'])
            
        form_data = self._create_signup_form(c2)

        pretty_btn = """

        <style>

        div[class="row-widget stButton"] > button {

            width: 100%;

        }

        </style>

        <br><br>

        """
        c2.markdown(pretty_btn,unsafe_allow_html=True)



        if form_data['submitted']:
            self._do_signup(form_data, c2)


    def _create_signup_form(self, parent_container) -> Dict:

        login_form = parent_container.form(key="login_form")

        form_state = {}
        form_state['username'] = login_form.text_input('Username')
        form_state['password'] = login_form.text_input('Password',type="password")
        form_state['password2'] = login_form.text_input('Confirm Password',type="password")
        form_state['access_level'] = login_form.selectbox('Example Access Level',(1,2))
        form_state['submitted'] = login_form.form_submit_button('Sign Up')

        if parent_container.button('Login',key='loginbtn'):
            # set access level to a negative number to allow a kick to the unsecure_app set in the parent
            self.set_access(0, None)

            #Do the kick to the signup app
            self.do_redirect()

        return form_state

    def _do_signup(self, form_data, msg_container) -> None:
        if form_data['submitted'] and (form_data['password'] != form_data['password2']):
            st.error('Passwords do not match, please try again.')
        else:
            with st.spinner("🤓 now redirecting to login...."):
                self._save_signup(form_data)
                time.sleep(2)

                #access control uses an int value to allow for levels of permission that can be set for each user, this can then be checked within each app seperately.
                self.set_access(0, None)

                #Do the kick back to the login screen
                self.do_redirect()

    def _save_signup(self, signup_data):
        #get the user details from the form and save somehwere

        #signup_data
        # this is the data submitted

        #just show the data we captured
        what_we_got = f"""

        captured signup details: \n

        username: {signup_data['username']} \n

        password: {signup_data['password']} \n

        access level: {signup_data['access_level']} \n

        """

        st.write(what_we_got)