3/2/18

OAuth Access Tokens or JSON Web Tokens (JWT) for Delivering a Secure API?

Let’s begin with what they mean.
First of all, it's not really an either or scenario with OAuth and JSON Tokens as they are compatible - wherein JWT is a token format used by the authentication framework, OAuth.
JWT is a compact and self-contained mechanism, digitally authenticated and trusted, for transmitting data between various parties. They are extremely easy to use and are mainly used for implementing stateless authentication mechanisms.
OAuth 2 offers authorization flows for Java application development, along with mobile devices. It works with a central authorization server, which is used by the client to obtain access to a protected resource placed on the server. The access is only possible using a valid access token issued in the form of JSON – but it has no definitive structure assigned to it. OAuth 2.0 defines a protocol for passing your access token within the Authorization header. The resource server validates the signature using the following information:
  • Client
  • Lifetime
  • Scopes
  • Roles

Similarities

  • Token Presentation to the server: JWT authentication and OAuth2 appear very similar in presenting the token to the Resource Server. However, the former does not specify the methods used to obtain the token. This is where OAuth fills the void by defining various ways in which the Client can obtain an access token from the Authorization Server.
  • Used across services: Both JWT and OAuth2 give you one authorization server to handle all Logins/Registrations and generates the token – making it easy for corporate systems to have a secure environment. It removes all connections between auth-server and other servers, other than the pre-defined public key.

Differences

JSON is less verbose, which makes it compact in size. It becomes a better choice to be used in HTML and HTTP environments. The use of JWT at Internet scale increases the ease of client-side processing of tokens on various platforms simultaneously.

Why Should We Use OAuth2?

OAuth2 aims to simplify authorization to access protected data. It allows you access your data but protects the owner’s account credentials in the process. It allows the user to access data from one web account to the other by simply giving out tokens, instead of credentials. The token grants access to certain specific resources for a specific duration, which can be shared with a third party. This temporary data transfer authorization is done without providing anyone with complete access to all the data. Users can share verifiable assertions about themselves using OAuth2, without releasing personally identifiable information.

Cons of OAuth2:

OAuth2 protocol requires a secret string obtained from the service to be delegated. Developers find it difficult to manage this string on mobile devices, as it can be easily found in the application and misused.

Cons of JWT:

Compromised Secret Key: The one key method makes it slightly less reliable. If the Key is leaked, it compromises the whole system.
No Push Messages: Due to the lack of session records in the DB, identifying a client per user is not possible – hence, we cannot push messages.

Conclusion:

Using OAuth2 setup for authenticating multiple API platforms and applications, both in both public and private settings is a much more sensible way of doing things. JSON web tokens can work as your bearer tokens here and make life easier.

2/28/18

HTTP endpoint to shut down a spring boot

Maven quick example for maven user to configure HTTP endpoint to shut down a spring boot web app using spring-boot-starter-actuator so that you can copy and paste:
1. Maven pom.xml:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2. application.properties:
#NEED auth  protected 
endpoints.shutdown.sensitive=true

#Enable shutdown endpoint
endpoints.shutdown.enabled=true
All endpoints are listed here:
3.Send a post method to shutdown the app:
curl -X POST localhost:port/shutdown

Security Note:

if you need the shutdown method auth protected, you may also need
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-security</artifactId>
</dependency>

management.security.enabled=true
security.user.name=admin
security.user.password=secret
management.security.roles=SUPERUSER

[Evolution] Error resolving 'outlook.office365.com': Name or service not known

Check below config to fix this bug:

$ cat /etc/resolv.conf
nameserver 127.0.1.1
multi on
order hosts,bind


$ cat /etc/nsswitch.conf
passwd:         compat
group:          compat
shadow:         compat
gshadow:        files

hosts:          files dns
networks:       files

protocols:      db files
services:       db files
ethers:         db files
rpc:            db files

netgroup:       nis

2/26/18

HP EliteBook 840 G4

System:    Host: dqvnHP Kernel: 4.13.0-32-generic x86_64 (64 bit gcc: 5.4.0)
           Desktop: Cinnamon 3.6.7 (Gtk 3.18.9-1ubuntu3.3) dm: lightdm Distro: Linux Mint 18.3 Sylvia
Machine:   System: HP (portable) product: HP EliteBook 840 G4 Chassis: type: 10
           Mobo: HP model: 828C v: KBC Version 45.3C Bios: HP v: P78 Ver. 01.08 date: 10/17/2017
CPU:       Dual core Intel Core i7-7500U (-HT-MCP-) cache: 4096 KB
           flags: (lm nx sse sse2 sse3 sse4_1 sse4_2 ssse3 vmx) bmips: 11616
           clock speeds: min/max: 400/3500 MHz 1: 2900 MHz 2: 2900 MHz 3: 2900 MHz 4: 2900 MHz
Graphics:  Card: Intel Device 5916 bus-ID: 00:02.0 chip-ID: 8086:5916
           Display Server: X.Org 1.18.4 drivers: intel (unloaded: fbdev,vesa)
           Resolution: 1920x1080@60.01hz, 1920x1080@60.00hz
           GLX Renderer: Mesa DRI Intel HD Graphics 620 (Kaby Lake GT2)
           GLX Version: 3.0 Mesa 17.2.8 Direct Rendering: Yes
Audio:     Card Intel Device 9d71 driver: snd_hda_intel bus-ID: 00:1f.3 chip-ID: 8086:9d71
           Sound: Advanced Linux Sound Architecture v: k4.13.0-32-generic
Network:   Card-1: Intel Ethernet Connection (4) I219-V
           driver: e1000e v: 3.2.6-k bus-ID: 00:1f.6 chip-ID: 8086:15d8
           IF: enp0s31f6 state: up speed: 1000 Mbps duplex: full mac: 
           Card-2: Intel Device 24fd driver: iwlwifi bus-ID: 02:00.0 chip-ID: 8086:24fd
           IF: wlp2s0 state: up speed: N/A duplex: N/A mac: 
Drives:    HDD Total Size: NA (-) ID-1: /dev/nvme0n1 model: N/A size: 256.1GB serial: N/A
Partition: ID-1: / size: 92G used: 35G (40%) fs: ext4 dev: /dev/nvme0n1p2
           ID-2: swap-1 size: 16.00GB used: 0.00GB (0%) fs: swap dev: /dev/dm-0
RAID:      System: supported: N/A
           No RAID devices: /proc/mdstat, md_mod kernel module present
           Unused Devices: none
Sensors:   System Temperatures: cpu: 39.0C mobo: 0.0C
           Fan Speeds (in rpm): cpu: N/A
Repos:     Active apt sources in file: /etc/apt/sources.list.d/chrome.list
           deb [arch=amd64] http: //dl.google.com/linux/chrome/deb/ stable main
           Active apt sources in file: /etc/apt/sources.list.d/official-package-repositories.list
           deb http: //packages.linuxmint.com sylvia main upstream import backport
           deb http: //archive.ubuntu.com/ubuntu xenial main restricted universe multiverse
           deb http: //archive.ubuntu.com/ubuntu xenial-updates main restricted universe multiverse
           deb http: //archive.ubuntu.com/ubuntu xenial-backports main restricted universe multiverse
           deb http: //security.ubuntu.com/ubuntu/ xenial-security main restricted universe multiverse
           deb http: //archive.canonical.com/ubuntu/ xenial partner
           Active apt sources in file: /etc/apt/sources.list.d/peterlevi-ppa-xenial.list
           deb http: //ppa.launchpad.net/peterlevi/ppa/ubuntu xenial main
           deb-src http: //ppa.launchpad.net/peterlevi/ppa/ubuntu xenial main
           Active apt sources in file: /etc/apt/sources.list.d/skype-stable.list
           deb [arch=amd64] https: //repo.skype.com/deb stable main
           Active apt sources in file: /etc/apt/sources.list.d/stebbins-handbrake-git-snapshots-xenial.list
           deb http: //ppa.launchpad.net/stebbins/handbrake-git-snapshots/ubuntu xenial main
           deb-src http: //ppa.launchpad.net/stebbins/handbrake-git-snapshots/ubuntu xenial main
           Active apt sources in file: /etc/apt/sources.list.d/webupd8team-java-xenial.list
           deb http: //ppa.launchpad.net/webupd8team/java/ubuntu xenial main
           deb-src http: //ppa.launchpad.net/webupd8team/java/ubuntu xenial main
Info:      Processes: 261 Uptime: 4 days Memory: 7697.3/15920.6MB
           Init: systemd v: 229 runlevel: 5 default: 2 Gcc sys: 5.4.0
           Client: Unknown python2.7 client inxi: 2.2.35

12/6/17

How to Create Wi-Fi Hotspot in Ubuntu 17.04 (Support Android)

How to Create Wi-Fi Hotspot in Ubuntu 17.04 (Support Android)

This quick tutorial is going to show you how to create a WiFi hotspot in Ubuntu 17.04 laptop with support for Android devices.
First of all you need to connect to your Ubuntu laptop to a wired network, disable Wifi, and then do following steps:
1. Go to top-right corner network applet, select Edit Connections from drop-down menu.
In pop-up window, click the Add button.
edit-network-connection
2. Then choose Wi-Fi in the next ‘choose a connection type dialog’.
choose-wifi
3. Now in the new created WiFi hotspot, do:
  • In Wi-Fi tab, type in Connection name, SSID, and choose mode HotSpot.
  • In Wi-Fi security tab, choose an encrypted type and set a password.
  • In IPv4 Settings tab, select mode “Share to other computers”
  • wifi-hotspot
4. After saved the changes, enable Wi-Fi in the network menu, and it should automatically connected to this hotspot.
If not, select ‘Connect to Hidden Wi-Fi Network’ from network menu, and choose the new created hotspot from drop-down menu:
hidden-network
5. Finally your network menu will look like:
connect-to-hotspot
On you mobile phone or other devices, select connect to this Wi-Fi hotspot and enjoy!

11/3/17

CORS with spring-boot and angularjs not working

Add this class near to the place you create Application.java

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class SimpleCORSFilter implements Filter {

private final Logger log = LoggerFactory.getLogger(SimpleCORSFilter.class);

public SimpleCORSFilter() {
log.info("SimpleCORSFilter init");
}

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {

HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;

response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With, remember-me");

chain.doFilter(req, res);
}

@Override
public void init(FilterConfig filterConfig) {
}

@Override
public void destroy() {
}

}

10/31/17

Find the Git Commit User — Jenkins Pipeline


I recently started to use Jenkins 2 Pipeline scripts to setup Continuous Delivery Process. I wanted to get the email of the Git user who has done the commit for that particular build. Initially idea was to use following global variables set by the Jenkins Git Plugin.
  • GIT_AUTHOR_NAME and GIT_COMMITTER_NAME
  • GIT_AUTHOR_EMAIL and GIT_COMMITTER_EMAIL
Somehow, things didn’t work as per the plugin documentation. So, I decided to use Git client with shell or batch scripts. Following is how it is done in both Linux and Windows environments.
In Windows — as usual some tricks required 
def commiterDetails = bat (
script: 'git --no-pager show -s --format=%%ae',
returnStdout: true
)
def commiter = extractCommiterEmail(commiterDetails)
def extractCommiterEmail(details) {
def arr = details.tokenize('\n')
def email = arr[2].trim()
return email
}

In Linux
committerEmail = sh (
script: 'git --no-pager show -s --format=\'%ae\'',
returnStdout: true
).trim()