-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOperatingSystem.java
More file actions
211 lines (168 loc) · 5.57 KB
/
OperatingSystem.java
File metadata and controls
211 lines (168 loc) · 5.57 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// The classes and/or objects participating in this pattern are:
// 1. Facade (Authentication)
// - knows which subsystem classes are responsible for a request.
// - delegates client requests to appropriate subsystem objects.
// 2. Subsystem classes (LDAPAuth, KerberosAuth, LocalAuth)
// - implement subsystem functionality.
// - handle work assigned by the Facade object.
// - have no knowledge of the facade and keep no reference to it.
// Selin Doğa Orhan
// Pluggable Authentication Mechanism
import java.util.Scanner;
class User {
private static int uniqueId = 0; // bu her oluşturulan User'ın unique bir id ye sahip olması için
private String name; // user name yani username
private String pwd; // user password
private int id; // user id
public User(String _name, String _pwd) {
this.name = _name;
this.pwd = _pwd;
this.id = uniqueId++;
}
public String getName() {return name;}
public int getId() {return id;}
}
class LDAPAuth extends AuthenticationMechanism{
private static final LDAPAuth instance = new LDAPAuth();
private static final int LDAPSessionId = 1000;
private LDAPAuth() {}
public static LDAPAuth getInstance() {
return instance;
}
@Override
protected void prepareAuthenticating() {
System.out.println("Authentication started for Lightweight Directory Access Protocol (LDAP) ...");
}
@Override
protected int authenticate(String name, String pwd) {
if(name.equals("ldap selin") && pwd.equals("ldap doga"))
return 0;
else return 1;
}
@Override
protected int getuid(String name) {
return 0;
}
@Override
protected int setuid(int uid) {
return uid + LDAPSessionId;
}
}
class KerberosAuth extends AuthenticationMechanism{
private static final KerberosAuth instance = new KerberosAuth();
private static final int KerberosSessionId = 2000;
private KerberosAuth(){}
public static KerberosAuth getInstance() {
return instance;
}
@Override
protected void prepareAuthenticating() {
System.out.println("Authentication started for third party authentication mechanism (Kerberos)");
}
@Override
protected int authenticate(String name, String pwd) {
if(name.equals("kerberos selin") && pwd.equals("kerberos doga"))
return 0;
else return 1;
}
@Override
protected int getuid(String name) {
return 0;
}
@Override
protected int setuid(int uid) {
return uid + KerberosSessionId;
}
}
class LocalAuth extends AuthenticationMechanism{
private static final LocalAuth instance = new LocalAuth();
private static final int LocalSessionId = 3000;
private LocalAuth(){}
public static LocalAuth getInstance() {
return instance;
}
@Override
protected void prepareAuthenticating() {
System.out.println("Authentication started on Local File System");
}
@Override
protected int authenticate(String name, String pwd) {
if(name.equals("local selin") && pwd.equals("local doga"))
return 0;
else return 1;
}
@Override
protected int getuid(String name) {
return 0;
}
@Override
protected int setuid(int uid) {
return uid + LocalSessionId;
}
}
// Facade. "Authentication"
// Facade ama yeni özellik ekliyor
// Tüm authenticate yöntemlerine burada bakıyor
class Authentication {
private static final Authentication instance = new Authentication();
private static int sessionId; // bu kullanıcı ve authentication özelinde unique oluyor
private LDAPAuth ldapAuth;
private KerberosAuth kerberosAuth;
private LocalAuth localAuth;
private Authentication() {
ldapAuth = LDAPAuth.getInstance();
kerberosAuth = KerberosAuth.getInstance();
localAuth = LocalAuth.getInstance();
}
public static Authentication getInstance(){
return instance;
}
private String getusername(){
Scanner input = new Scanner(System.in);
System.out.println("Enter your name please, then press enter: ");
String r = input.nextLine();
return r;
}
private String getpassword(){
Scanner input = new Scanner(System.in);
System.out.println("Enter your password please, then press enter: ");
String ar = input.nextLine();
return ar;
}
public void LogIn(){
System.out.println("Welcome, You will be logging in to your OS. \n\tTo get started you need to enter your login credentials.");
String name = getusername();
String pwd = getpassword();
// alınan isim ve password tüm authentication yöntemlerinde bakılıyor
int ldap = ldapAuth.startAuthentication(name, pwd);
if(ldap == 0){
System.out.println("Authentication successful in LDAP.");
int uid = ldapAuth.getuid(name);
sessionId = ldapAuth.setuid(uid);
} else { // ...
int kerberos = kerberosAuth.startAuthentication(name, pwd);
if(kerberos == 0){
System.out.println("Authentication successful in Kerberos.");
int uid = kerberosAuth.getuid(name);
sessionId = kerberosAuth.setuid(uid);
} else {
int local = localAuth.startAuthentication(name, pwd);
if(local == 0){
System.out.println("Authentication successful in Local File System.");
int uid = localAuth.getuid(name);
sessionId = localAuth.setuid(uid);
}
else{
System.out.println("Your authentication failed for all authentication mechanisms.\nYour login credentials are not found in any authentication mechanism. \n\tTherefore you are not logged in. Sorry...");
}
}
}
}
}
public class OperatingSystem {
public static void main(String[] args) {
//Facade
Authentication authentication = Authentication.getInstance();
authentication.LogIn();
}
}