In this example, we create an authorization service that is capable of serving as an OAuth2 authorization server.
- Golang 1.20+
- GNU Makefile
- Docker for Mac
$GOBIN
&$GOROOT
are correctly setGO111MODULE
is "on"
In this example, the source code contains the fully finished service. This step by step will walk through the steps involved in writing this service.
This involves the following steps. For detail explanation of each step, see the developer guide
- Create Module.yml
- Add go.mod
- Add Makefile
- call
shell make init CLI_TAG="develop"
to initialize the project
Add the main file corresponding to the definition in Module.yml. The main file is the entry point for this service.
The main method implementation is boilerplate. Its only purpose is to start the application execution. The OAuth2 features
are configured via the serviceinit.Use()
call.
Add pkg/init
directory. In this directory, the package.go
file implements the Use()
method. In this method,
all the go-lanai
packages that this service needs are declared.
The authserver_configurer.go
, serserver_configurer.go
and security_configurer.go
methods provides further customization to the
declared packages. In authserver_configurer.go
, we provide the implementations that the security packages requires.
The security packages requires the application to provide implementation to interfaces. For example the AccountStore
implementation tells the security package how to look up a user. In this example, all the implementation are in memory.
They are implemented in the pkg/service
directory.
go run cmd/auth-service/main.go
Navigate to http://localhost:8900/auth/login, you will see the login page. To see the auth service in full action, run it
together with another example such as the database example. Auth service will be used to authenticate the user
in those examples. See configs/application.yml
's security.in-memory.accounts
property to see the user you can use with this example.
Generate the private key:
openssl genrsa -out jwtkeys_example.pem -aes256 1024
Extract the public key:
+openssl rsa -in jwtkeys_example.pem -pubout > jwtpubkey_example.pem
Generate the private key:
openssl genrsa -out saml_example.key -aes256 1024
Generate the certificate:
openssl req -key saml_example.key -new -x509 -days 365 -out saml_example.cert