JAAS etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
JAAS etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

10 Mart 2020 Salı

JAAS Principal Arayüzü

Giriş
Şu satırı dahil ederiz.
import javax.security.Principal;
Açıklaması şöyle. Subject nesnesi içinde saklanırlar. Bence ismi Privilege olsaydı daha iyi olurdu. Bu Subject'in şu işi yapmaya hakkı var mı sorusuna cevap veriyor
A Subject is a Java object that represents a single entity, such as an individual. A single Subject can have a number of associated identities, each of which is represented by a Principal object. So, say a single Subject represents an employee who requires access to both the e-mail system and the accounting system. That Subject will have two Principals, one associated with the employee's user ID for e-mail access and the other associated with his user ID for the accounting system.

Principals are not persistent, so they must be added to the Subject each time the user logs in. A Principal is added to a Subject as a part of a successful authentication procedure. Likewise, a Principal is removed from the Subject if the authentication fails. Regardless of the success or failure of authentication, all Principals are removed when the application performs a logout.

13 Şubat 2020 Perşembe

JAAS LoginModule Arayüzü - Bu Sınıfı Kendimiz Yazarız

Giriş
Şu satırı dahil ederiz.
import javax.security.login.LoginModule;
Açıklaması şöyle. JVM ile bazı hazır LoginModule sınıfları geliyor.
JAAS provides some reference LoginModule implementations, such as the JndiLoginModule; you can also develop your own,...
Açıklaması şöyle. İstersek kendi LoginModule sınıfımızı yazarız.
This interface is primarily meant for JAAS providers. It allows JAAS providers to implement and plug in authentication mechanisms as login modules. LoginModules are plugged into an application environment to provide a particular type of authentication. In an authentication process, each LoginModule is initialized with a Subject, a CallbackHandler, shared LoginModule state, and LoginModule-specific options. The LoginModule uses the CallbackHandler to communicate with the user. J2SE 1.4 provides a number of LoginModules bundled under the com.sun.security.auth.module package.
Önce her modülün initialize() + login() metodları çağrılır. Login başarılı ise true döner. Daha sonra her modülün commit() metodu çağrılır.

Dosya
Şöyle yaparız. Dosyanın ismi MyLoginJAAS.conf olsun
MyLogin { 
  com.foo.bar.MyLoginModule1 required doNotCheck=true;
  com.foo.bar.MyLoginModule2 optional;
};
abort metodu
İmzası şöyle.
public boolean abort() throws LoginException;
initialize metodu
İmzası şöyle.
public void initialize(Subject subject, CallbackHandler callbackHandler, 
  Map<String, ?> sharedState, Map<String, ?> options);
- Parametre olarak geçilen subject nesnesini sınıf içindeki bir değişkende saklamak gerekir.
- Parametre olarak geçilen callbackHandler nesnesini sınıf içindeki bir değişkende saklamak gerekir.
- options nesnesi -Djava.security.auth.login.config=/conf/MyJaas.config seçeneği ile verilen ayarları içerir.

login metodu
İmzası şöyle.
public boolean login() throws LoginException;
init() metodu ile bize verilen ve sakladığımız callbackHandler nesnesinin handle() metodunu kendi javax.security.auth.callback.Callback nesnelerimiz ile çağırırız. Her callback bize bir sonuç döner. Örneğin ilk callback kullanıcı adını döndürür. İkinci callback ise şifreyi döndürür.
Bu bilgileri kullanarak kullanıcıyı doğrularız.

Eğer kullanıcıyı doğrulayabilirsek bunu bir değişken içinde ismi loginVerification olan bir değişken içinde saklarız. Bu değişkenin amacı commit() metodu çağrılınca login() metodunun sonucunu hatırlamaktır.

Eğer kullanıcıyı doğrulayamazsak RuntimeLoginException() fırlatırız.

Eğer LoginModule sınıfımızın dikkate alınmasını istemiyorsak bu metoddan false döneriz.
Örnek
Şöyle yaparız.
public boolean login() throws LoginException {

  try {
    // Setup the callbacks to obtain user authentication information
    Callback myCallback[] = new Callback[2];         
    myCallback[0] = new NameCallback(" Enter Username:");
    myCallback[1] = new PasswordCallback("Enter Password:", false);

    // Invoke the handle() method in the Callbackhandler
    CallbackHandler.handle(myCallback);

    // Get the username and password from the callback
    myName = ((NameCallback) myCallback[0]).getName();
    myPassword = new String ((PasswordCallback)myCallback[1]).getPassword());

    // Perform a simple verification
    if (( myName.equals("ramesh"))&& (myPassword.equals("javaguy"))) {             
      loginVerification = true;
    }
    else {
      loginVerification = false;
    }
  } catch (Exception e) {
    throw new LoginException("Login failed")
  }
  return true;
}
logout metodu
İmzası şöyle.
public boolean logout() throws LoginException;
initialize metodunda sakladığımız subject nesnesine ait java.security.Principal nesnelerini sileriz. Şöyle yaparız.
public boolean logout() throws LoginException
{
  subject.getPrincipals().removeAll(principals);
  principals.clear();
  return true;
}

JAAS Subject Sınıfı

Giriş
Şu satırı dahil ederiz.
import javax.security.auth.Subject;
Açıklaması şöyle.
Represents a group of related entities, such as people, organizations, or services with a set of security credentials. Once authenticated, a Subject is populated with associated identities, or Principals. The authorization actions will be made based on the Subject.
getPublicCredentials metodu
Örnek ver

getPrincipals metodu
Set<Principal> nesnesi döner. Principal yazısına bakabilirsiniz.

Örnek ver.

7 Mart 2019 Perşembe

JAAS Kullanımı

Giriş
Java Authentication and Authorization Service (JAAS) hem authentication hem de authorization için kullanılır.

Projemizde JAAS kullanmak istiyorsak önce JVM'i çalıştırırken şöyle bir parametre veririz.
-Djava.security.auth.login.config=/conf/MyJaas.config
Bu dosya şöyledir
MyJaasLogin {

 com.foo.bar.LoginModule1 REQUIRED
 com.foo.bar.LoginModule2 REQUIRED myoption=true
 com.foo.bar.LoginModule3 OPTIONAL
}
Bu dosyada LoginModule arayüzünden kalıtan sınıflarımız bulunur. Açıklaması şöyle
Your application-layer code deals primarily with a LoginContext. Underneath that LoginContext is a set of one or more dynamically configured LoginModules, which handle the actual authentication using the appropriate security infrastructure.
REQUIRED değerinden sonra gelen string LoginModule arayüzünden kalıtan sınıfın initialize() metoduna geçilir. Metodun imzası şöyle
public void initialize(Subject subject,CallbackHandler callbackHandler,
  Map<String,?> sharedState,Map<String,?> options);
options parametresi kullanılarak okunabilir.

Adımlar
Açıklaması şöyle.
Using JAAS authentication from your application typically involves the following steps:

1. Create a LoginContext
2. Optionally pass a CallbackHandler to the LoginContext, for gathering or processing authentication data
3. Perform authentication by calling the LoginContext's login() method
4. Perform privileged actions using the returned Subject (assuming login succeeds)
LoginContext Yaratma
Açıklaması şöyle
1. During initialization, the LoginContext finds the configuration entry "MyExample" in a JAAS configuration file (which you configured) to determine which LoginModules to load (see Figure 2)

2. During login, the LoginContext calls each LoginModule's login() method

3. Each login() method performs the authentication or enlists a CallbackHandler

4. The CallbackHandler uses one or more Callbacks to interact with the user and gather input

5. A new Subject instance is populated with authentication details such as Principals and credentials

28 Haziran 2018 Perşembe

JAAS NameCallback Sınıfı

Giriş
Şu satırı dahil ederiz.
import javax.security.auth.calllback.NameCallback;
Callback arayüzünden kalıtır.

Kullanım
Kullanıcı ismini elde etmek içindir. Nesneyi doldurmak için şöyle yaparız.
CallbackHandler callbackHandler = ...;

// prepare callback objects and get the authentication information

Callback[] callbacks = new Callback[2];
callbacks[0] = new NameCallback("Username: ");
callbacks[1] = new PasswordCallback("Password: ", false);

try {
  callbackHandler.handle(callbacks);
}
catch (Exception e) {
  throw new LoginException(e.getMessage());
}
constructor
Şöyle yaparız.
NameCallback nameCallback = new NameCallback("Username: ");
getName metodu
Şöyle yaparız.
String userName = nameCallback.getName();

JAAS LoginContext Sınıfı

Giriş
Şu satırı dahil ederiz.
import javax.security.login.LoginContext;
LoginModule arayüzlerinin çağrılmasını sağlar. Açıklaması şöyle.
In Java SE, the typical / intended usage of the SPI is via the LoginContext class, which, by default, lazily instantiates and initializes each of its configured modules once, just before delegating to their login method. Subsequently, the context retains references to the very same module instances until its disposal. Hence, the lifecycle of a module depends on the lifetime of its encapsulating context.
İyi bir örnek burada.

constructor
Örnek
Şöyle yaparızz. MyLoginJAAS.conf dosyasındaki her bir LoginModule üzerinden yürüyerek, login işlemini gerçekleştirir.
CallbackHandler handler = myView;
LoginContext context = new LoginContext("MyLogin", handler);
contex.login();

getSubject metodu
Subject nesnesi döner. Açıklaması şöyle.
JAAS also borrows ideas from other established security frameworks, such as X.509 certificates, from which the name Subject is derived....
Örnek ver

login metodu
JAAS.conf dosyasındaki her bir LoginModule üzerinden yürüyerek, login işlemini gerçekleştirir.