Posts in Category "Java"

My First Book – “Mastering OAuth 2.0”

Here at Adobe, I work on the Identity Platform team. We are responsible for running the main identity management platform that services all of Adobe’s customers. As part of this, we get the opportunity to solve some very interesting problems in the identity space. I’ve written about this before, but we recently underwent a large change to our identity platform to adopt the OAuth 2.0 standard for authentication and authorization. This allows our users to integrate with us much like they would with other large service providers who have also adopted OAuth 2.0, such as Google, Microsoft, and Facebook. This was a very fun project and one that I particularly enjoyed because I learned a heck of a lot about identity management and API security, as well as some of the deeper ins and outs of the OAuth 2.0 protocol. At the conclusion of that project, I was approached by the nice folks at Packt Publishing to write a book about the topic.

Introducing Mastering OAuth 2.0
I present to you, my first book, Mastering OAuth 2.0…

download sample chapter

What does it cover?
The book covers what you would imagine: the OAuth 2.0 protocol and how to use it effectively. However, it focuses specifically on the client integration side of the OAuth 2.0 protocol. That is, this book is geared towards application developers looking to build applications that integrate with OAuth 2.0 service providers, as opposed to application developers looking to build their own OAuth 2.0 servers. This is best described by the book’s description…

“OAuth 2.0 is a powerful authentication and authorization framework that has been adopted as a standard in the technical community. Proper use of this protocol will enable your application to interact with the world’s most popular service providers, allowing you to leverage their world-class technologies in your own application. Want to log your user in to your application with their Facebook account? Want to display an interactive Google Map in your application? How about posting an update to your user’s LinkedIn feed? This is all achievable through the power of OAuth. With a focus on practicality and security, this book takes a detailed and hands-on approach to explaining the protocol, highlighting important pieces of information along the way.”

If you are an application developer looking to build a web or mobile application that integrates with any of the major service providers, like Google or Facebook, you will likely be doing it via OAuth 2.0. If so, this book is right up your alley :)

The World’s Most Interesting Infographic
One thing that I’m especially proud of regarding this book is the depth of the code and sample applications that I provide. I didn’t want to step through the learning process while building a sample application that isn’t realistic, or that no one will ever use. Rather, I decided to build an interesting, real-world, working sample application. Enter The World’s Most Interesting Infographic!

The World’s Most Interesting Infographic is the sample application that is built throughout the book. It is a web application that will, through the power of OAuth 2.0, request profile and feed data from your Facebook account and dynamically generate an infographic based on the statistics it gleans. Here is a snapshot of mine…

The World's Most Interesting Infographic

Log into www.worldsmostinterestinginfographic.com (or www.wmiig.com for short) to see your own infographic! Or better yet, follow along in the book and create your own application!

It’s all open-source!
Much like I do with a lot of my other projects, I’ve made the source code for The World’s Most Interesting Infographic, as well as the rest of the sample code for the book, available on GitHub. Check it out here…

See me on GitHub

Sample chapter
We’ve made chapter 1 available for free for anyone interested in checking the book out, but not sure whether it will be useful for them. You can download it in PDF form, or view it on the publisher’s interactive website…

Download sample chapter as PDF

View sample chapter on publisher website

Take a look at the sample chapter I’ve provided above, and if you like it, please consider purchasing it! As always, I love hearing from you. So do let me know your feedback in the comments too. Happy coding!

Charles

My Adventure Writing My First Quine in Java

Today, I tried to write my very first quine. What is a quine, you might ask? Well, for those that don’t know, a quine is simply a program that takes no input but whose only output is the program code itself. Easy, right? That’s what I thought. This post is going to document my adventure writing my first quine in Java.

 

Trial and error

So, I need to write a program that outputs a program…itself to be exact. I didn’t think much of it, so I dove right in. Let’s take a look at my first pass…

public class Quine {
	public static void main(String[] args) {
		System.out.println("public class Quine {\n\tpublic static void main(String[] args) {\n\t\tSystem.out.println(\"...fuck");
	}
}
 
/* OUTPUT:
public class Quine {
	public static void main(String[] args) {
		System.out.println("...fuck
 
*/

Fuck. This won’t work. As you can see, the problem quickly reveals itself. Essentially, there comes a point where you must print the code that you are using to print! (Even that sentence is a little confusing) This can easily degrade into an infinite loop. So, how do you overcome this?

You must write some code that you use to both store the characters
to be printed, but can ALSO be used to print itself!

I believe this is the main problem to solve when writing quines. Once you can get over this hump, the rest is simple! If you can figure this out, you’ve done 90% of the work. Really! So I started to tackle this problem, head on.

 

A string that prints itself

First, I did away with the System.out.println() method. Instead, I opted for the printf() method. With functionality very similar to the ANSII C printf() method, it has some powerful string manipulation features that will make this quine much easier to write (and read). Second, I tried to think of a way to write some code that printed itself. More specifically, I decided that I needed to figure out a way to solve the following code segment…

String s = "?";
System.out.printf(?);

What could I replace the question marks with such that it would output the string declaration itself, exactly, and nothing else? I thought about this for a while before I came up with a solution. Since I think that this step is the most important and requires the most thinking, rather than ruin it for you, I’ll let you think about it for yourself.

 

*SPOILER ALERT – ANSWER BELOW*


 

 

 

 
Willard Van Orman Quine - A Harvard Professor of philosophy for which the self-reproducing code concept was named after

Willard Van Orman Quine - A Harvard professor of philosophy for which the self-reproducing code concept was named after



 

 

 

 

After some head-scratching, I came up with…

public class Quine {
	public static void main(String[] args) {
		String s = "String s = %c%s%c;";
		System.out.printf(s, '"', s, '"');
	}
}
 
/* OUTPUT:
String s = "String s = %c%s%c;";
*/

There are many other solutions for this, but this is what I came up with. Now that I’ve got this figured out, all that’s left is filling in the stuff before and after.

 

All that’s left is everything

The self-reproducing code that I wrote above is essentially the middle of the program, and I’ve figured out a way to print it. Now, on to printing the stuff that goes before. This is quite a bit simpler than the first step, but not quite trivial. Again, here is another failed attempt…

public class Quine {
	public static void main(String[] args) {
		String s = "public class Quine {\n\tpublic static void main(String[] args) {\n\t\tString s = %c%s%c;";
		System.out.printf(s, '"', s, '"');
	}
}
 
/* OUTPUT:
public class Quine {
	public static void main(String[] args) {
		String s = "public class Quine {
	public static void main(String[] args) {
		String s = %c%s%c;";
*/

You’ll notice that hardcoding the line-feeds and horizontal tabs into the string doesn’t give us the right result. They get evaluated by the printf() twice. The solution to this is to make use of the variable arguments in printf() and reference those characters instead.

public class Quine {
	public static void main(String[] args) {
		String s = "public class Quine {%c%cpublic static void main(String[] args) {%c%c%cString s = %c%s%c;";
		System.out.printf(s, '\n', '\t', '\n', '\t', '\t', '"', s, '"');
	}
}
 
/* OUTPUT:
public class Quine {
	public static void main(String[] args) {
		String s = "public class Quine {%c%cpublic static void main(String[] args) {%c%c%cString s = %c%s%c;";
*/

Nice! Things are coming along, but the code is getting uglier.

 

Spring cleaning!

As you can see, we’re starting to get a lot of redundancy in the printf() call. My next step was to clean this up. Quine’s are all about reuse! The less code there is, the less code I have to worry about re-printing. So, I changed all of the arguments to printf() to be index-based instead, allowing me to reuse characters.

public class Quine {
	public static void main(String[] args) {
		String s = "public class Quine {%3$c%4$cpublic static void main (String[] args) {%3$c%4$c%4$cString s = %2$c%1$s%2$c;";
		System.out.printf(s, s, '"', '\n', '\t');
	}
}
 
/* OUTPUT:
public class Quine {
	public static void main (String[] args) {
		String s = "public class Quine {%3$c%4$cpublic static void main (String[] args) {%3$c%4$c%4$cString s = %2$c%1$s%2$c;";
*/

This is looking a lot cleaner now. Finally, let’s round this quine off by filling in the rest of the code.

 

Print the rest...easy! I think.

Again, this is quite a bit simpler than the previous steps, but not trivial. Another failed attempt…

public class Quine {
	public static void main(String[] args) {
		String s = "public class Quine {%3$c%4$cpublic static void main (String[] args) {%3$c%4$c%4$cString s = %2$c%1$s%2$c;%3$c%4$c%4$cSystem.out.printf(s, s, ...fuck";
		System.out.printf(s, s, '"', '\n', '\t');
	}
}
 
/* OUTPUT:
public class Quine {
	public static void main (String[] args) {
		String s = "public class Quine {%3$c%4$cpublic static void main (String[] args) {%3$c%4$c%4$cString s = %2$c%1$s%2$c;%3$c%4$c%4$cSystem.out.printf(s, s, ...fuck";
		System.out.printf(s, s, ...fuck
*/

We have hit the familiar problem of printing code that is used to print itself! This is slightly different, however. The difference is that we are trying to output characters that must be escaped in one output, but not in the other. There is a simple solution for this! Use the ASCII character codes instead! Replacing ‘”‘, ‘\n’, and ‘\t’ with their ASCII decimal equivalent codes, we are easily able to insert them into the string literal without them being interpreted by the printf() method, and without the need for escaping them. In the end, we get….

public class Quine {
	public static void main(String[] args) {
		String s = "public class Quine {%3$c%4$cpublic static void main (String[] args) {%3$c%4$c%4$cString s = %2$c%1$s%2$c;%3$c%4$c%4$cSystem.out.printf(s, s, 34, 10, 9);%3$c%4$c}%3$c}";
		System.out.printf(s, s, 34, 10, 9);
	}
}
 
/* OUTPUT:
public class Quine {
	public static void main (String[] args) {
		String s = "public class Quine {%3$c%4$cpublic static void main (String[] args) {%3$c%4$c%4$cString s = %2$c%1$s%2$c;%3$c%4$c%4$cSystem.out.printf(s, s, 34, 10, 9);%3$c%4$c}%3$c}";
		System.out.printf(s, s, 34, 10, 9);
	}
}
*/

Eureka! My very first quine! Try writing one for yourself! Feel free to share them with me in the comments, or through e-mail at charlesb[at]adobe[dot]com! I’d love to post them :)

Enjoy!

 
Charles