-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateHexaEightSession.cs
134 lines (117 loc) · 6.51 KB
/
CreateHexaEightSession.cs
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
using DustInTheWind.ConsoleTools;
using DustInTheWind.ConsoleTools.Controls;
using HexaEightJose;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
namespace HexaEight_Sessions_Demo
{
partial class HexaEightUser
{
public void Createsession()
{
try
{
Console.Clear();
CustomConsole.WriteLine();
CustomConsole.WriteLine(HorizontalAlignment.Center, "Create New HexaEight Session");
CustomConsole.WriteLine();
HorizontalLine.QuickDisplay();
CustomConsole.WriteEmphasized("Enter your Email ID To Get Started : ");
var useremail = Console.ReadLine();
Console.WriteLine("");
if (useremail.Trim() != "")
{
CustomConsole.WriteEmphasized("Enter a strong remember-able password to secure this Session :");
var clientpassword = session.ReadPassword();
Console.WriteLine("");
CustomConsole.WriteEmphasized("Re-Enter your password For Verification : ");
var rclientpassword = session.ReadPassword();
Console.WriteLine("");
if (clientpassword.Trim() == rclientpassword.Trim())
{
if (clientpassword.Length >= 15)
{
//Initialize the Client Program by passing the Client ID and URL of the Authentication Server
session.InititalizeClient(clientId, tokenServerURL, "");
//Inititalize a new HexaEight Session
if (session.InitiateHexaEightSession())
{
//Use the useremail and password to fetch a QR Code that can be used to authenticate the user
var qrcoderesp = session.FetchLoginQRCode(useremail, clientpassword).Result;
//Display the QR Code to the user - This uses the console to display a QR Code
session.DisplayLoginQRCode(qrcoderesp);
Thread.Sleep(8000);
//Once the user authenticates using HexaEight Authenticator, fetch the user JWT token
var jwtsession = session.FetchJWTToken(qrcoderesp, useremail, clientpassword).Result;
//Verify and Save the jwttoken so that it can be used by the user to login to Client Program anytime using the emailid and password
session.InititalizeClient(clientId, tokenServerURL, jwtsession);
var resourceidentity = session.FetchResourceIdentity();
if (resourceidentity != "")
{
try
{
System.IO.Directory.CreateDirectory("." + Path.DirectorySeparatorChar + "tokens");
}
finally
{
CustomConsole.WriteLine();
Console.WriteLine("Saving Session Token... ");
CustomConsole.WriteLine();
}
if (System.IO.File.Exists("." + Path.DirectorySeparatorChar + "tokens" + Path.DirectorySeparatorChar + JWT.Get512Hash(JWT.Get512Hash(useremail))))
{
System.IO.File.Delete("." + Path.DirectorySeparatorChar + "tokens" + Path.DirectorySeparatorChar + JWT.Get512Hash(JWT.Get512Hash(useremail)));
}
var file = System.IO.File.CreateText("." + Path.DirectorySeparatorChar + "tokens" + Path.DirectorySeparatorChar + JWT.Get512Hash(JWT.Get512Hash(useremail)));
file.WriteLine(jwtsession);
file.Flush();
file.Close();
CustomConsole.WriteLineSuccess("Success : Session Created Successfully. The JWT session token is displayed below.");
CustomConsole.WriteLine();
CustomConsole.WriteLineSuccess("Paste the below token in a browser to view its contents...");
CustomConsole.WriteLine();
HorizontalLine.QuickDisplay();
CustomConsole.WriteLineSuccess("https://jwt.io/#debugger-io?token=" + jwtsession);
HorizontalLine.QuickDisplay();
}
else
{
CustomConsole.WriteLineError("Error : Unable To Create a New Session. Try again");
CustomConsole.WriteLine();
}
}
else
{
CustomConsole.WriteLineError("Error : Session Initialization Error. Try again");
CustomConsole.WriteLine();
}
}
else
{
CustomConsole.WriteLineError("Error : Password Length Insufficient. Minimum 15 Characters");
CustomConsole.WriteLine();
}
}
else
{
CustomConsole.WriteLineError("Error : Passwords dont match!! Try again.");
CustomConsole.WriteLine();
}
}
else
{
CustomConsole.WriteLineError("Error : Email Addresses dont match!! Try again.");
CustomConsole.WriteLine();
}
}
catch (Exception ex)
{
CustomConsole.WriteLineError("Error : Unable to create Session. Try again [" + ex.Message + "]");
CustomConsole.WriteLine();
}
}
}
}