Saturday, 24 June 2017

OAuth2.0 Basics

oAuth2.0 is a standard authorization protocol. According to the official definition

The OAuth2.0 authorization framework enables third party application to obtain limited access to HTTP service, either on behalf of the resource owner or by orchestrating an approval process between the resource owner and the HTTP service, or by allowing the third party application to obtain access on its own behalf.

Some important terms associated with OAuth2.0

1. Roles
* Resource Owner: An entity capable of granting access to protected resource.
* Resource Server: The server hosting the protected resource
* Client: Application trying to access protected resource.
* Authorization server: The server issuing access token after successful authentication.

2. There are four type of grant.
* authorization code
* implicit
* resource owner credentials(username/password)
* client credentials

3. OAuth2 token
* Access Token: Sent with each request, short live
* Refresh Token: Used to generate access token. Long lfe.

4.Access token scope: Scope showing what access rights were actually granted to the client




Type of grant are important as they dictate the flow in OAuth2.0

1.Authorization Code Grant: 

This type of grant is used to receive both access token and refresh token. This is a redirection based flow so
A.Client must be capable of interacting with resource owner with user-agent such as web browser
B.Client should be capable of receiving incoming request from authorization server



Steps
 1. Client initiates the flow through client agent(web browser) to authorization server. Information from client sent are client Id, request scope, local state, redirection uri.
 2. Authentication server authenticate with resource owner who uses user-agent to approve or deny access.
 3. If resource owner grant the access, authorization server uses the redirect uri to send back authorization code and any local state to the client.
 4. Client retrieve the authorization code from the redirect uri and request for access token.
 5. Authentication server validates all the information sent by client and if everything is as expected send back the access token and optionally refresh token.


2.Implicit Grant

Implicit Grant has following characteristic
    A. We have user-agent based clients such as Single Page Application that cannot keep the client secret because all the code is accessible
B. Instead of authorization server returning authorization code, it return access token and also no refresh token is returned


Steps

1. Client initiates the flow through client agent(web browser) to authorization server. Information from client sent are client Id, request scope, local state, redirection uri.
 2. Authentication server authenticate with resource owner who uses user-agent to approve or deny access.
1.Authentication server send access token in redirection uri in fragment to client agent
2.The user-agent follows the redirection instructions by making a request to the web-hosted client resource
3.The web-hosted client resource returns a web page capable of access full redirection uri for extracting the access token
4.Script is executed in the client agent to extract access token
5.Access token is passed to client application.


3.Resource username/password

This type of grant is applicable when there is trust relationship between resource owner and client. This happens when client have access to username/password of the resource owner.





4.Client Credentials

  This type of grant is useful when client has control on the resource it is trying to access.Client receive the access token directly from the authentication server



Wednesday, 21 June 2017

Queue

Queue is a data structure that supports first in first out (FIFO). Elements that are inserted first are extracted last.

Couple of important method for queue are
1. Enqueue: Add element to queue
2. Dequeue: Remove element from queue.

package queue;

import java.util.ArrayList;

public class Queue {
private ArrayList<Object> queue=new ArrayList<>();

public Queue() {
super();
}
/**
 * Add object to the queue
 * @param o
 */
public void enqueue(Object o){
queue.add(o);
System.out.println("Object added "+o);
}

/**
 * remove object from the queue. 
 * remove only if element is there is queue.
 */
public void dequeue(){
//check the size of the queue before removing element from queue
if(queue.size()>0){
System.out.println("Element removed "+queue.get(0));
queue.remove(0);
}
else{
System.out.println("Queue is empty");
}
}
}


Test Program

package queue;

public class MyQueue {

public static void main(String[] args) {
Queue queue=new Queue();
queue.dequeue();
queue.enqueue("A");
queue.enqueue("B");
queue.enqueue("C");
queue.dequeue();
queue.enqueue("D");
queue.enqueue("E");
queue.dequeue();
queue.dequeue();
queue.dequeue();
queue.dequeue();
queue.dequeue();

}

} 

Result

Queue is empty
Object added A
Object added B
Object added C
Element removed A
Object added D
Object added E
Element removed B
Element removed C
Element removed D
Element removed E
Queue is empty

Thursday, 8 June 2017

Stack

Stack is a data structure that support Last In First Out . This implies that last element that is added to the data structure is extracted  first(LIFO )

Some of the important method of stack are as follows
1. Push: Method to add element to stack
2. Pop: Method to remove element from the stack
3. Peek: View the topmost element of the stack.

Below is the stack implementation using ArrayList.

package stack;

import java.util.ArrayList;

public class Stack {

private ArrayList<Object> stack = new ArrayList<>();

public Stack() {
super();
}

public Stack(ArrayList<Object> stack) {
super();
this.stack = stack;
}

/**
 * The method that add element to the top of the stack
 * 
 * @param o
 */
public void push(Object o) {
stack.add(o);
System.out.println("element added " + o);
}

/**
 * method that remove the element from top of stack
 */
public void pop() {

System.out.println("stack size " + stack.size());
// Ensure that we try to remove element from stack only when there is element in stack
// This condition is necessary to avoid null pointer exception
if (stack.size() > 0) {
System.out.println("element removed " + stack.get(stack.size() - 1));
stack.remove(stack.size() - 1);

else {
System.out.println("stack is empty");
}
}

/**
 * Method to check the topmost element of the stack
 * 
 * @return
 */
public Object peek() {
Object o = null;
// Ensure that we try to access element from stack only when there is element in stack 
// This condition is necessary to avoid null pointer exception
if (stack.size() > 0) {

o = stack.get(stack.size() - 1);
System.out.println("element at top " + o);

else {
System.out.println("stack is empty");
}

return o;

}

}

Test class
package stack;

public class MyStack {

public static void main(String[] args) {
Stack stack=new Stack();
stack.peek();
stack.push("A");
stack.push("B");
stack.push("C");
stack.peek();
stack.pop();
stack.peek();
stack.push("D");
stack.peek();
stack.pop();
stack.pop();
stack.pop();
stack.peek();
}


}

Output


element at top B
element added D
element at top D
stack size 3
element removed D
stack size 2
element removed B
stack size 1
element removed A
stack is empty
  


Sunday, 4 June 2017

Cloning with Cloneable interface


Cloning is the process of making copy of an object. To enable cloning of a class we have to do following
1.Implements the cloneable interface
2.Override the clone method from Object class.

Clone object must follow below characteristic
1.O1.clone()!=O1 
This implies that they original object and cloned object are in different heap space.
2.O1.clone().getClass()==O1.getClass
    This implies that both the original and the cloned object are of same type but this is not an absolute requirement
3.O1.clone().equals(O1)
This implies that original and cloned object are equal but this is no ant absolute requirement.

Shallow Cloning
Shallow cloning is the default behavior of cloning where there is bitwise copying of original object   to generate cloned object. This means all the primitive variable are copied by value while any inner object are copied by reference. This has the implication that original and cloned object share the same inner object. Hence any change in the inner object by original impacts cloned object and vice verse. 



 Illustration





    Test class: 



Default implementation of cloning should be used when the member of cloneable class are of primitive type or immutable object. 


Deep cloning
If we want that original and cloned object are completely independent of each other we have to implement deep cloning. 




Illustration
Note the change in implementation of clone method of Person class.













Also note that inner object Address now implement cloneable interface and also provide implementation for the clone method. 
This has implication that for deep cloning all the nested class should implement cloneable interface and also provide implementation for clone();


 

Test class will remain same and we get the below result.



Resources

Streaming with Kafka API

The Kafka Streams API is a Java library for building real-time applications and microservices that efficiently process and analyze large-sca...