11 Eylül 2020 Cuma

HTTP Basic Authentication Nedir?

Giriş
Basic Authentication server-to-server authentication için kullanılır. Basic Authentication genellikle Microservice mimarisinde ve TLS/HTTPS ile kullanılır. Açıklaması şöyle.
HTTP basic auth sends credentials un-hashed and unencrypted over the wire, relying on TLS for confidentiality in transit. 
htpasswd
Eğer Apache sunucusu kullanıyorsak, HTTP Basic Authentication ile gelen şifreleri saklamak için htpasswd kullanılabilir.

Microservice Mimarisinde Basic Authentication Dezavantajları
Açıklaması şöyle.
- In each request, it is necessary to pass the user's credentials and sensitive information. The less sensitive information is sent, the better.

- Contrary to what many people imagine, Base 64 is not an encryption, and it is very easy to decode it to find the user's data from it.

- Thinking of a distributed architecture with microservices, all requests will have to carry out a validation to authenticate and authorize the user, which may result in an overload in the security service.
Buradaki ilk maddeyi daha iyi açıklayan bir paragraf şöyle. Yani şifreyi paylaştığımız sunucu sandığımız kişi olmayabilir, bunu doğrulamanın yolu yok
Even with authenticated entities, we should not share secrets. Protocols, like HTTP basic authentication, are outdated because the client shares their secret with the verifier in order to get authenticated. There are many crypto protocols out there that are based on zero-knowledge proofs (asymmetric cryptography) or require the client to prove that they have the secret by asking it to hash a nonce with the secret. Such protocols and methods should be the basis of the future authentication standards in cloud native and microservices based architectures.

OAuth2 İle Farkı
Basic Authentication doğrulama yöntemi yerine artık çoğunlukla OAuth2 kullanılıyor. Açıklaması şöyle.
Before OAuth 2.0, the way developers handled server-to-server authentication was with HTTP Basic Auth. Essentially, this boiled down to a developer that would send over a server’s unique username and password (often referred to as an ID and secret) on each request. The API service would then validate this username and password on every request by connecting to a user store (database, LDAP, etc.) in order to validate the credentials.
OAuth2 ile http isteği şöyle görünüyor.
GET /resource HTTP/1.1
Host: server.example.com
Authorization: Bearer mF_9.B5f-4.1JqM
Digest Authentication İle Farkı
HTTP Basic Authentication' da kullanıcı adı ve şifre her istekte gönderilir. Digest Authentication da kullanıcı adı ve şifre her istekte gönderilmez.

Dolayısıyla Basic Authentication yönteminin bir eksisi, her istekte credentials' ın tekrar gönderilmesi, tekrar authenticate edilmesi. Bu işlemin CPU maliyeti var. 

Akış
Akış kabaca şöyle.
1. İstemci HTTP Get isteğini gönderir.
2. Sunucu "401 Unauthorized WWW-Authenticate : Basic realm="..." şeklinde cevap verir. Bu aslında bana kullanıcı + şifreni gönder anlamına gelir.
3. İstemci tekrar Get isteği gönderir ancak bu sefer Authorization : Basic Base64 olarak kodekli kullanıcı: şifre bilgisini gönderir
4. Sunucu Http 200 OK veya Http 403 Forbidden ile cevap verir.

Alanlar
1. Basic Authentication için Http Request Header alanında Authorization alanı "Basic" + Base64 olarak "kullanıcı : şifre" şeklinde doldurulmalıdır.

Örnek
Şöyle yaparız.
Accept: application/json, text/plain, */*
Authorization: Basic aW4yOG1pbnV0ZXM6ZHVtbXk=

Kodla Basic Authentication Yapmak

C#
C#'taki WebRequest sınıfı, Credentials alanı doldurulsa bile Http Request Header' daki Authentication alanını göndermiyor. RCF 2617'ye uygun olarak
1. Önce istekte bulunuyor.
2. Sunucu 401 ile cevap veriyor.
3. Sonra Authorization alanını doldurarak tekrar istekte bulunuyor.
C#'ta bu gitgellerin olmaması için direkt Authorization alanını doldurulur. Şöyle yaparız.
void SetBasicAuthHeader(WebRequest req, String userName, String userPassword)
{
  string authInfo = userName + ":" + userPassword;

  authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));

  req.Headers["Authorization"] = "Basic " + authInfo;
}
Spring
SpringSecurity HttpBasicConfigurer Sınıfı yazısına bakabilirsiniz.

Browser ile Basic Authentication Yapmak
Eğer browser kullanıyorsak, HTTP Basic Authentication' da browser bir login popup penceresi çıkarır. İstemci kullanıcı adı ve şifresini istek ile gönderir. Bilgiler request header ile gönderilir. Sunucu Login veya session başlatmaz. Bu yöntemin bir dezavantajı tarayıcının kullanıcı adı ve şifresini sürekli hatırlamasıdır. Yani klasik log-out imkanı bulunmaması.


Hiç yorum yok:

Yorum Gönder