Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: ci

on:
pull_request:
branches: [main]

jobs:
tests:
name: Tests
runs-on: ubuntu-latest

steps:
- name: Check out code
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: "1.25.1"

- name: Run unit test
run: go test -cover ./...
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# learn-cicd-starter (Notely)

This repo contains the starter code for the "Notely" application for the "Learn CICD" course on [Boot.dev](https://boot.dev).
![CI](https://github.com/Basel-Saadeh/learn-cicd-starter/actions/workflows/ci.yml/badge.svg)

This repo contains the starter code for the "Notely" application for the "Learn CICD" course on [Boot.dev](https://boot.dev).gi

## Local Development

Expand All @@ -21,3 +23,4 @@ go build -o notely && ./notely
*This starts the server in non-database mode.* It will serve a simple webpage at `http://localhost:8080`.

You do *not* need to set up a database or any interactivity on the webpage yet. Instructions for that will come later in the course!
Basel Saadeh's Version of Boot.dev's Notely app.
27 changes: 14 additions & 13 deletions internal/auth/auth.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
package auth

import (
"errors"
"net/http"
"strings"
"errors"
"net/http"
"strings"
)

var ErrNoAuthHeaderIncluded = errors.New("no authorization header included")

// GetAPIKey -
func GetAPIKey(headers http.Header) (string, error) {
authHeader := headers.Get("Authorization")
if authHeader == "" {
return "", ErrNoAuthHeaderIncluded
}
splitAuth := strings.Split(authHeader, " ")
if len(splitAuth) < 2 || splitAuth[0] != "ApiKey" {
return "", errors.New("malformed authorization header")
}
authHeader := strings.TrimSpace(headers.Get("Authorization"))
if authHeader == "" {
return "", ErrNoAuthHeaderIncluded
}

return splitAuth[1], nil
parts := strings.Fields(authHeader)
if len(parts) != 2 || parts[0] != "ApiKey" {
return "", errors.New("malformed authorization header")
}

key := strings.TrimSpace(parts[1])
return key, nil
}
29 changes: 29 additions & 0 deletions internal/auth/auth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package auth

import (
"net/http"
"testing"
)

func TestGetAPIKey_ValidHeader(t *testing.T) {
h := http.Header{}
h.Set("Authorization", "ApiKey 12345")

key, err := GetAPIKey(h)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if key != "12345" {
t.Errorf("expected key '12345', got '%s'", key)
}
}

func TestGetAPIKey_MissingPrefix(t *testing.T) {
h := http.Header{}
h.Set("Authorization", "12345")

_, err := GetAPIKey(h)
if err == nil {
t.Fatal("expected an error, got nil")
}
}