authn-middleware

command
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 4, 2024 License: MIT Imports: 8 Imported by: 0

README

AuthN Middleware Example

The example below demonstrates how to create a simple HTTP server that authenticates requests with JWT tokens.

Usage

To be able to run this example, you need to have the IDP test server running (see IDP Test Server Example for more details).

go run ../idp-test-server/main.go

To start the server, run the following command:

go run main.go

The server will start on port 8080.

Trying to access the service without a token
$ curl 127.0.0.1:8080
{"error":{"domain":"MyService","code":"bearerTokenMissing","message":"Authorization bearer token is missing."}}

Service logs:

$ curl 127.0.0.1:8080
{"error":{"domain":"MyService","code":"bearerTokenMissing","message":"Authorization bearer token is missing."}}
Trying to access the service with an invalid token
$ curl -H "Authorization: Bearer invalid-token" 127.0.0.1:8080
{"error":{"domain":"MyService","code":"authenticationFailed","message":"Authentication is failed."}}

Service logs:

{"level":"error","time":"2024-10-06T23:47:11.554193+03:00","msg":"authentication failed","pid":76054,"request_id":"","int_request_id":"","trace_id":"","error":"token is malformed: token contains an invalid number of segments"}
{"level":"error","time":"2024-10-06T23:47:11.554201+03:00","msg":"error in response","pid":76054,"request_id":"","int_request_id":"","trace_id":"","error_code":"authenticationFailed","error_message":"Authentication is failed."}
{"level":"info","time":"2024-10-06T23:47:11.554226+03:00","msg":"response completed in 0.000s","pid":76054,"request_id":"","int_request_id":"","trace_id":"","method":"GET","uri":"/","remote_addr":"127.0.0.1:61538","content_length":0,"user_agent"
:"curl/8.7.1","remote_addr_ip":"127.0.0.1","remote_addr_port":61538,"duration_ms":0,"duration":299,"status":401,"bytes_sent":100}
Doing a successful request
$ curl -XPOST -u user:user-pwd 127.0.0.1:8081/idp/token
{"access_token":"eyJhbGciOiJSUzI1NiIsImtpZCI6ImZhYzAxYzA3MGNkMDhiYTA4ODA5NzYyZGE2ZTRmNzRhZjE0ZTQ3OTAiLCJ0eXAiOiJhdCtqd3QifQ.eyJpc3MiOiJodHRwOi8vMTI3LjAuMC4xOjgwODEiLCJzdWIiOiJ1c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ta7LQHqzfvpXuAkB0iJFYLsCDs4A"
Hello, user

Service logs:

{"level":"info","time":"2024-10-06T23:48:14.106249+03:00","msg":"2 keys fetched (jwks_url: http://127.0.0.1:8081/idp/keys)","pid":76054}
{"level":"info","time":"2024-10-06T23:48:14.10667+03:00","msg":"response completed in 0.002s","pid":76054,"request_id":"","int_request_id":"","trace_id":"","method":"GET","uri":"/","remote_addr":"127.0.0.1:61581","content_length":0,"user_agent":"curl/8.7.1","remote_addr_ip":"127.0.0.1","remote_addr_port":61581,"duration_ms":2,"duration":2116,"status":200,"bytes_sent":11}

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL