<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Neha Srivastava</title>
    <description>The latest articles on DEV Community by Neha Srivastava (@neha-sri).</description>
    <link>https://dev.to/neha-sri</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1065219%2F80547a77-e1e6-4450-906d-dd104d4d985c.jpeg</url>
      <title>DEV Community: Neha Srivastava</title>
      <link>https://dev.to/neha-sri</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/neha-sri"/>
    <language>en</language>
    <item>
      <title>Basic Kotlin/Java + Spring app with GitHub OAuth 2.0</title>
      <dc:creator>Neha Srivastava</dc:creator>
      <pubDate>Sat, 15 Apr 2023 04:58:56 +0000</pubDate>
      <link>https://dev.to/neha-sri/basic-kotlinjava-spring-app-with-github-oauth-20-3905</link>
      <guid>https://dev.to/neha-sri/basic-kotlinjava-spring-app-with-github-oauth-20-3905</guid>
      <description>&lt;p&gt;Kotlin and Spring Boot are extremely powerful ecosystems that provide a lot of functionality out of the box.&lt;/p&gt;

&lt;p&gt;Authenticating with GitHub OAuth is very easy but it appears that there is a lack of basic guides on how to set one up through OAuth. Believe me, I tried.&lt;/p&gt;

&lt;p&gt;(Even GPT sent me down a rabbit hole and I’ve now spent my whole day, debugging and fixing the example it gave — PS: Didn’t work but I learnt enough to build one on my own.) So here goes nothing.&lt;/p&gt;

&lt;h2&gt;
  
  
  5 Steps to Success
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Step 1: Setup your OAuth Application on GitHub
&lt;/h3&gt;

&lt;p&gt;In order to authenticate using GitHub, you need to register an Application on your GitHub account.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Login to &lt;a href="https://github.com" rel="noopener noreferrer"&gt;https://github.com&lt;/a&gt;. Click on your user settings on the top-right.&lt;/li&gt;
&lt;li&gt;Go to &lt;em&gt;Settings -&amp;gt; Developer Settings -&amp;gt; OAuth Apps -&amp;gt; New OAuth App&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: If you are authenticating the user for an organization, you must register the application under the organization’s GitHub account and not your personal one.&lt;br&gt;
The setup for this can be found through:&lt;br&gt;
&lt;em&gt;Settings -&amp;gt; Your Organizations -&amp;gt; Select organization -&amp;gt; Settings -&amp;gt; Developer Settings -&amp;gt; OAuth Apps -&amp;gt; New OAuth App&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;You would need to have admin-access to the organization.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Enter the following details. Hit Register Application. We explain the meaning of the obscure fields next.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0b4m2yumn8kd4u7prsto.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0b4m2yumn8kd4u7prsto.png" alt="GitHub App Registration"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Homepage URL:&lt;/strong&gt; For the purposes of our demo, we will use &lt;a href="http://localhost:8080" rel="noopener noreferrer"&gt;http://localhost:8080&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Authorization Callback URL:&lt;/strong&gt; For the purposes of our demo, we will use &lt;a href="http://localhost:8080/login/oauth2/code/github" rel="noopener noreferrer"&gt;http://localhost:8080/login/oauth2/code/github&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This URL is the default endpoint that Spring Security uses to receive the authorization code from the GitHub authorization server after the user grants access to your application. The code parameter in the URL indicates that the server is returning an authorization code instead of an access token.&lt;/p&gt;

&lt;p&gt;Once Spring Security receives the authorization code, it exchanges it for an access token by sending a token request to the GitHub token endpoint, and uses the access token to authenticate and authorize the user.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: This is the most important step. Frankly, every broken tutorial I found on GitHub OAuth appeared broken because of a mismatch between the value of what they said should be entered here and what they wrote in code. Happens, but be careful so you don’t spend infinite time like I did.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Hit Register application.&lt;/li&gt;
&lt;li&gt;This will take you to the application settings page. You would need to create a new client secret.&lt;/li&gt;
&lt;li&gt;Note down client secret in a secure place as it will only be displayed only once.&lt;/li&gt;
&lt;li&gt;You would need client id, client secret and the authorization callback url in Step 3 of the tutorial.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Step 2: Create a new Spring Application
&lt;/h3&gt;

&lt;p&gt;There are many ways to do this. I am using Spring Initializer, which in my opinion is the easiest way specially for those new to Spring framework .&lt;/p&gt;

&lt;p&gt;Optionally, you could also create a new Spring project from your IDE and add the dependencies in the gradle or maven project. (code below).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The easiest way to do this is to use Spring Initializr. I am using Gradle but you can use Maven or Groovy as you like.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Select Kotlin in the language section. I’m using Spring Boot 3.0.5 with Jar packaging and Java 17. Enter desired package and group names for your project.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the Dependencies section, Add “Spring Web” and “OAuth2 Client”.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click Generate at the bottom. This will prompt a download in the .zip format. When you unzip, it will create the whole top-level project structure so no need to create a folder before hand. Just unzip and open the project in your favorite IDE.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbjm9olwrcqwubxrz21in.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbjm9olwrcqwubxrz21in.png" alt="Spring Initializr"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This step will create the &lt;code&gt;build.gradle.kts&lt;/code&gt; file for us which would look like this.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins &lt;span class="o"&gt;{&lt;/span&gt;
 &lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"org.springframework.boot"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; version &lt;span class="s2"&gt;"2.7.10"&lt;/span&gt;
 &lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"io.spring.dependency-management"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; version &lt;span class="s2"&gt;"1.0.15.RELEASE"&lt;/span&gt;
 kotlin&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"jvm"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; version &lt;span class="s2"&gt;"1.6.21"&lt;/span&gt;
 kotlin&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"plugin.spring"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; version &lt;span class="s2"&gt;"1.6.21"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

group &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"com.nsri"&lt;/span&gt;
version &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"0.0.1-SNAPSHOT"&lt;/span&gt;
java.sourceCompatibility &lt;span class="o"&gt;=&lt;/span&gt; JavaVersion.VERSION_17

repositories &lt;span class="o"&gt;{&lt;/span&gt;
 mavenCentral&lt;span class="o"&gt;()&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

dependencies &lt;span class="o"&gt;{&lt;/span&gt;
 implementation&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"org.springframework.boot:spring-boot-starter-oauth2-client"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
 implementation&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"org.springframework.boot:spring-boot-starter-web"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
 implementation&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"com.fasterxml.jackson.module:jackson-module-kotlin"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
 implementation&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"org.jetbrains.kotlin:kotlin-reflect"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
 testImplementation&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"org.springframework.boot:spring-boot-starter-test"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

tasks.withType&amp;lt;KotlinCompile&amp;gt; &lt;span class="o"&gt;{&lt;/span&gt;
 kotlinOptions &lt;span class="o"&gt;{&lt;/span&gt;
  freeCompilerArgs &lt;span class="o"&gt;=&lt;/span&gt; listOf&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"-Xjsr305=strict"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  jvmTarget &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"17"&lt;/span&gt;
 &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

tasks.withType&amp;lt;Test&amp;gt; &lt;span class="o"&gt;{&lt;/span&gt;
 useJUnitPlatform&lt;span class="o"&gt;()&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This project should build after importing in your IDE.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3: Create &lt;code&gt;application.yaml&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Kotlin and Spring Boot often use some default project structure to do default wirings. We will store our settings in the recommended directory as per Spring:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;src/main/resources/application.yaml&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Replace the details in &amp;lt;&amp;gt; below with your client id, client secret and redirect-uri from Step 1.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;

&lt;span class="na"&gt;spring&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;security&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;oauth2&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;client&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;registration&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;github&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
            &lt;span class="na"&gt;client-id&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;&amp;lt;client-secret-from-github-oauth-setup-step&amp;gt;&lt;/span&gt;
            &lt;span class="na"&gt;client-secret&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;&amp;lt;client-secret-from-github-oauth-setup-step&amp;gt;&lt;/span&gt;
            &lt;span class="na"&gt;redirect-uri&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;&amp;lt;redirect-uri-from-github-oauth-setup-step&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;# http://localhost:8080/login/oauth2/code/github&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If you know how to re-wire Spring to use files from your custom structure using annotations, feel free to add the settings file wherever you usually do.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 4: Create &lt;code&gt;index.html&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Arguably this is not necessary to demonstrate OAuth or Spring Security but it is nice to have a custom page so you can be sure that authentication worked.&lt;/p&gt;

&lt;p&gt;We will store our HTML landing page in &lt;code&gt;src/main/resources/static/index.html&lt;/code&gt;&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;

&lt;p&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;&lt;br&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;&lt;br&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;&lt;br&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Welcome to Demo&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;&lt;br&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;&lt;br&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;&lt;br&gt;
&lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Welcome to GitHub OAuth Demo&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;&lt;br&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;&lt;br&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;&lt;/p&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Step 5: Create &lt;code&gt;OAuthDemoApplication.kt&lt;/code&gt;&lt;br&gt;
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: If you used Spring Initializr or IDE to generate the Spring project, it is likely that it created the main application for you. If so, you can skip to running the project.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Create the main application file under &lt;code&gt;src/main/kotlin/&amp;lt;package name&amp;gt;/OAuthApplicationDemo.kt&lt;/code&gt; (if it was not already generated).&lt;/p&gt;

&lt;p&gt;It should look like this.&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;

&lt;p&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;com.nsri.oauthdemo&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.boot.autoconfigure.SpringBootApplication&lt;/span&gt;&lt;br&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.boot.runApplication&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class="nd"&gt;@SpringBootApplication&lt;/span&gt;&lt;br&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OauthDemoApplication&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;br&gt;
 &lt;span class="n"&gt;runApplication&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;OauthDemoApplication&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;br&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;/p&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  You’re done! Run the application&lt;br&gt;
&lt;/h3&gt;

&lt;p&gt;That’s it. As you can see, there is barely any Kotlin specific code here so you can use the same steps for Java as well.&lt;/p&gt;

&lt;p&gt;Run your application using &lt;code&gt;gradle-bootrun&lt;/code&gt; or directly from the IDE.&lt;/p&gt;

&lt;p&gt;In your browser, launch &lt;a href="http://localhost:8080" rel="noopener noreferrer"&gt;http://localhost:8080&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you are not authenticated, you will be directed to a page by with Github authentication screen like below. (The url will redirect to Github). Enter your credentials (including 2FA) to login as usual.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fucdyf8qawp5t0yumnqql.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fucdyf8qawp5t0yumnqql.png" alt="GitHub Authorization Page on localhost:8080"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After login, you will be taken to index.html.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqk32zk9vs3pki2a34kv5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqk32zk9vs3pki2a34kv5.png" alt="Welcome Page (index.html)"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you are already authenticated in this browser session, you will be directly taken to &lt;code&gt;index.html&lt;/code&gt;. This is expected (and honestly the point of OAuth2.0) because it is a type of “Single Sign-On”, which means it remembers your login if you have already authenticated.&lt;/p&gt;

&lt;p&gt;For testing purposes, use a private/incognito window of your browser or clear browser history and cookies and launch &lt;code&gt;localhost:8080&lt;/code&gt; again.&lt;/p&gt;

&lt;p&gt;All done! 🎉&lt;/p&gt;

&lt;p&gt;Hope you found this tutorial helpful and it saved you some time.&lt;/p&gt;

&lt;p&gt;(All the code is available on the oauth-demo repository on &lt;a href="https://github.com/neha-sri/oauth-demo" rel="noopener noreferrer"&gt;Github&lt;/a&gt;.)&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>spring</category>
      <category>security</category>
      <category>github</category>
    </item>
  </channel>
</rss>
