<?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>Forem: hanapiko</title>
    <description>The latest articles on Forem by hanapiko (@hanapiko).</description>
    <link>https://forem.com/hanapiko</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%2F1991763%2F5bc9a875-a845-4cc3-9be4-ec3d2100f81e.JPG</url>
      <title>Forem: hanapiko</title>
      <link>https://forem.com/hanapiko</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/hanapiko"/>
    <language>en</language>
    <item>
      <title>A Deep Dive into Secure Authentication 🛡️💻</title>
      <dc:creator>hanapiko</dc:creator>
      <pubDate>Fri, 13 Jun 2025 06:54:36 +0000</pubDate>
      <link>https://forem.com/hanapiko/a-deep-dive-into-secure-authentication-2idp</link>
      <guid>https://forem.com/hanapiko/a-deep-dive-into-secure-authentication-2idp</guid>
      <description>&lt;p&gt;A few months ago, I had the opportunity to co-present a tech talk titled “JWTs in Go: A Deep Dive into Secure Authentication” And honestly? It was one of those sessions where you walk away having learned just as much as you shared.&lt;/p&gt;

&lt;p&gt;We explored the world of JSON Web Tokens (JWTs), their role in securing modern applications, and how to implement them effectively in Go.This article is a reflection of that session, peppered with lessons and some personal takeaways that stuck with me.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Exactly Are JWTs?
&lt;/h2&gt;

&lt;p&gt;JWTs are like digital passports they help two parties trust each other without constantly asking “Who are you again?” They consist of three parts:&lt;/p&gt;

&lt;p&gt;Header.Payload.Signature&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Header: Info about the token algorithm, token type.

Payload: The claims. Think user ID, roles, or anything relevant.

Signature: Ensures that no one tampered with the token.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;They’re encoded (not encrypted!) and meant to be verified, not trusted blindly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why JWTs in Go?
&lt;/h2&gt;

&lt;p&gt;Go is fast, clean, and great for building APIs, perfect match for JWTs. Instead of maintaining clunky server-side sessions, you issue a token and validate it with every request. Stateless. Scalable. Simple.&lt;/p&gt;

&lt;p&gt;But, and this is a big but, it’s only simple if you implement it right.&lt;/p&gt;

&lt;h2&gt;
  
  
  🔐 How JWT Authentication Works in Go
&lt;/h2&gt;

&lt;p&gt;Here’s a super simplified flow:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- User logs in with credentials.

- Server validates credentials and generates a JWT.

- User stores the token (usually in localStorage or a cookie).

On every API request, the token is sent in the Authorization header.

- Server validates the token and either allows or denies access.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We used the popular github.com/golang-jwt/jwt/v5 package to do the heavy lifting.&lt;/p&gt;

&lt;h2&gt;
  
  
  Generating a Token
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func GenerateJWT(username string) (string, error) {
     claims := jwt.RegisteredClaims{
        Subject:   username,
        ExpiresAt: jwt.NewNumericDate(time.Now().Add(2 * time.Hour)),
        IssuedAt:  jwt.NewNumericDate(time.Now()),
    }

    token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
    return token.SignedString([]byte("your-secret-key"))
}

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Validating a Token
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func ValidateJWT(tokenStr string) (*jwt.RegisteredClaims, error) {
    token, err := jwt.ParseWithClaims(tokenStr, &amp;amp;jwt.RegisteredClaims{}, func(t *jwt.Token) (interface{}, error) {
        return []byte("your-secret-key"), nil
    })

    if err != nil || !token.Valid {
        return nil, err
    }

    claims, ok := token.Claims.(*jwt.RegisteredClaims)
    if !ok {
        return nil, errors.New("invalid claims")
    }

    return claims, nil
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Hard Earned Lessons
&lt;/h3&gt;

&lt;p&gt;Some mistakes we’ve either made or seen others make:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;❌ Storing sensitive data (like passwords!) in the JWT payload. Never do this.

❌ Forgetting to set expiration times (exp) - leads to tokens that never die.

❌ Using weak secrets. A JWT signed with mysecret123 is an open invitation to attackers.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Instead:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;✅ Use short-lived access tokens + refresh tokens.

✅ Secure your secrets with env vars or vaults.

✅ Always validate all claims, even the ones you don’t think matter.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;🔄 Bonus: Token Refreshing&lt;/p&gt;

&lt;p&gt;If you’ve ever had to juggle access + refresh tokens, you know it’s not trivial. Go doesn’t magically handle this, you have to build a strategy.&lt;/p&gt;

&lt;h3&gt;
  
  
  Final Thoughts
&lt;/h3&gt;

&lt;p&gt;JWTs are powerful, but they’re not magic. Go gives you the tools to implement them efficiently, but security is always in the details.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Never trust a token blindly.

Always validate everything.

And don’t forget the human side of engineering, code is only part of the equation.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>learning</category>
      <category>tutorial</category>
      <category>cybersecurity</category>
      <category>authentication</category>
    </item>
    <item>
      <title>WebSocket vs HTTP: Understanding the Real Difference</title>
      <dc:creator>hanapiko</dc:creator>
      <pubDate>Thu, 22 May 2025 12:47:10 +0000</pubDate>
      <link>https://forem.com/hanapiko/websocket-vs-http-understanding-the-real-difference-628</link>
      <guid>https://forem.com/hanapiko/websocket-vs-http-understanding-the-real-difference-628</guid>
      <description>&lt;p&gt;Ever built something that needed real-time updates and found yourself wondering if HTTP alone could cut it? I’ve been there too. In this post, I’ll walk you through the key differences between HTTP and WebSocket, technologies you’ll often encounter when building modern web applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What’s the Core Difference?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Both HTTP and WebSocket are protocols used for communication between a client and a server. But they operate very differently and that matters a lot depending on what you're building.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;HTTP: Request-Response, Stateless&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;HTTP is the way the web communicates. A client (like your browser) sends a request, and the server sends a response. That’s it. Once the transaction is done, the connection usually closes.&lt;/p&gt;

&lt;p&gt;Great for:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- Web pages

- REST APIs

- Static resources
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Not so great for:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- Anything that needs real-time data updates
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;WebSocket: Full-Duplex, Persistent&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;WebSocket is designed for continuous, two-way communication. After an initial handshake over HTTP, the connection upgrades to WebSocket and stays open. Both client and server can push messages to each other anytime.&lt;/p&gt;

&lt;p&gt;Perfect for:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- Live chat

- Online games

- Stock tickers

Collaborative editing tools

HTTP runs securely over HTTPS. Well supported, widely used, and easy to scale.

WebSocket uses WSS (WebSocket Secure) for encrypted traffic. Requires a bit more care in implementation, especially with authentication and origin checks.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Both protocols are supported by all modern browsers and most cloud/server infrastructures.&lt;br&gt;
💡 So… When Should You Use Each?&lt;/p&gt;

&lt;p&gt;If your app:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Only needs data when a user does something (like clicking a button)

Is stateless or RESTful
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;✅ Use HTTP&lt;/p&gt;

&lt;p&gt;If your app:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Needs to send or receive data without user action

Updates in real-time
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;🔥 Go with WebSocket&lt;br&gt;
🧠 Final Thoughts&lt;/p&gt;

&lt;p&gt;Whether you're building a chat app or just exploring backend development, understanding the difference between HTTP and WebSocket will help you design smarter, more efficient systems.&lt;/p&gt;

&lt;p&gt;Let me know your thoughts or experiences in the comments&lt;/p&gt;

</description>
      <category>programming</category>
      <category>coding</category>
      <category>webdev</category>
      <category>websocket</category>
    </item>
    <item>
      <title>Introduction to Runes</title>
      <dc:creator>hanapiko</dc:creator>
      <pubDate>Wed, 16 Oct 2024 06:08:02 +0000</pubDate>
      <link>https://forem.com/hanapiko/introduction-to-runes-2a7l</link>
      <guid>https://forem.com/hanapiko/introduction-to-runes-2a7l</guid>
      <description>&lt;p&gt;When programming, dealing with text is a common task. Different programming languages have various ways to handle text, and Go (also known as Golang) has a special concept called "runes" to make working with characters easier. This guide will help you understand what runes are, why they’re important, and how to use them in Go.&lt;br&gt;
What is a Rune?&lt;/p&gt;

&lt;p&gt;In Go, a rune is a type that represents a single character. But it’s more than just a letter or symbol; a rune corresponds to a Unicode code point. Unicode is a system that assigns a unique number to every character from different languages and symbols.&lt;/p&gt;

&lt;p&gt;Here’s a simple breakdown:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Characters: These are the letters, numbers, and symbols you see in text, like 'A', '1', or '#'.
Unicode: This system gives each character a unique number. For example, 'A' has a Unicode code point of U+0041.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In Go, a rune is just an int32, which means it can store any Unicode character.&lt;br&gt;
Why Use Runes?&lt;/p&gt;

&lt;p&gt;Runes are useful for several reasons:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Support for Multiple Languages: Runes allow you to handle characters from many languages and symbols, making your programs more versatile.
Clear Code: When you use runes, it’s clear that you’re dealing with characters, not just raw bytes.
Text Manipulation: Runes make it easier to work with text, especially when dealing with complex characters that take up more than one byte.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Basic Rune Operations&lt;/p&gt;

&lt;p&gt;Here’s how to work with runes in Go:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Declaring Runes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You can declare a rune in Go using single quotes. For example:&lt;/p&gt;

&lt;p&gt;go&lt;/p&gt;

&lt;p&gt;var r rune = 'A'&lt;/p&gt;

&lt;p&gt;This creates a rune variable r with the character 'A'.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Printing Runes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;To print a rune, use the %c format in fmt.Printf to show the character. For example:&lt;/p&gt;

&lt;p&gt;go&lt;/p&gt;

&lt;p&gt;package main&lt;/p&gt;

&lt;p&gt;import "fmt"&lt;/p&gt;

&lt;p&gt;func main() {&lt;br&gt;
    var r rune = 'A'&lt;br&gt;
    fmt.Printf("Rune: %c, Unicode: %U\n", r, r)&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;Rune: A, Unicode: U+0041&lt;/p&gt;

&lt;p&gt;This shows both the character and its Unicode value.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Working with Strings and Runes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In Go, strings are sequences of bytes, which can be tricky when handling characters that use multiple bytes. To handle this, you can use runes:&lt;/p&gt;

&lt;p&gt;go&lt;/p&gt;

&lt;p&gt;package main&lt;/p&gt;

&lt;p&gt;import "fmt"&lt;/p&gt;

&lt;p&gt;func main() {&lt;br&gt;
    str := "Hello, 世界" // "Hello, World" in Mandarin&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for i, r := range str {
    fmt.Printf("%d: %c (Unicode: %U)\n", i, r, r)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;Output:&lt;br&gt;
0: H (Unicode: U+0048)&lt;br&gt;
1: e (Unicode: U+0065)&lt;br&gt;
2: l (Unicode: U+006C)&lt;br&gt;
3: l (Unicode: U+006C)&lt;br&gt;
4: o (Unicode: U+006F)&lt;br&gt;
5: , (Unicode: U+002C)&lt;br&gt;
6:   (Unicode: U+0020)&lt;br&gt;
7: 世 (Unicode: U+4E16)&lt;br&gt;
9: 界 (Unicode: U+754C)&lt;/p&gt;

&lt;p&gt;This code shows how to loop through each character in a string, including multi-byte characters.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Rune Slices
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You can also create a slice of runes, which is useful for working with a collection of characters. Here’s how to convert a string into a rune slice:&lt;/p&gt;

&lt;p&gt;go&lt;/p&gt;

&lt;p&gt;package main&lt;/p&gt;

&lt;p&gt;import "fmt"&lt;/p&gt;

&lt;p&gt;func main() {&lt;br&gt;
    str := "Go is fun!"&lt;br&gt;
    runeSlice := []rune(str)&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for i, r := range runeSlice {
    fmt.Printf("Index %d: %c\n", i, r)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;Output:&lt;br&gt;
Index 0: G&lt;br&gt;
Index 1: o&lt;br&gt;
Index 2:&lt;br&gt;&lt;br&gt;
Index 3: i&lt;br&gt;
Index 4: s&lt;br&gt;
Index 5:&lt;br&gt;&lt;br&gt;
Index 6: f&lt;br&gt;
Index 7: u&lt;br&gt;
Index 8: n&lt;br&gt;
Index 9: !&lt;/p&gt;

&lt;p&gt;This shows each character in the string "Go is fun!" as a separate rune.&lt;br&gt;
&lt;strong&gt;Practical Uses of Runes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Understanding runes is useful in several scenarios:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Supporting Multiple Languages: When your application needs to handle different languages, runes help manage text accurately.
Text Processing: For tasks like searching or formatting text, runes make it easier to work with individual characters.
Command-Line Tools: If your tool processes text input, runes ensure it works correctly with various characters.
Emoji Handling: Since emojis are Unicode characters, runes help you include them in your text without issues.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Conclusion&lt;/p&gt;

&lt;p&gt;Runes are a powerful feature in Go that simplify working with text and characters. By representing each character as a Unicode code point, runes help you manage text in a flexible and clear way. With this guide, you should have a good start on understanding and using runes in your Go programs&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
