OAuth 2.0 Authorization Server & Authorization Middleware for the Iris Web Framework.
This library offers an OAuth 2.0 Authorization Server based on Iris and an Authorization Middleware usable in Resource Servers developed with Iris.
The Authorization Server is implemented by the struct OAuthBearerServer that manages two grant types of authorizations (password and client_credentials). This Authorization Server is made to provide authorization token usable for consuming resources API.
OAuthBearerServer supports password grant type allowing the token generation for username / password credentials.
OAuthBearerServer supports client_credentials grant type allowing the token generation for client_id / client_secret credentials.
These grant types are currently not supported.
If authorization token will expire, client can regenerate the token calling the authorization server and using the refresh_token grant type.
The Iris middleware BearerAuthentication intercepts the resource server calls and authorizes only resource requests containing a valid bearer token.
Authorization Server crypts the token using the Token Formatter and Authorization Middleware decrypts the token using the same Token Formatter. This library contains a default implementation of the formatter interface called SHA256RC4TokenSecureFormatter based on the algorithms SHA256 and RC4. Programmers can develop their Token Formatter implementing the interface TokenSecureFormatter and this is really recommended before publishing the API in a production environment.
The interface CredentialsVerifier defines the hooks called during the token generation process. The methods are called in this order:
- ValidateUser() or ValidateClient() called first for credentials verification
- AddClaims() used for add information to the token that will be encrypted
- StoreTokenId() called after the token generation but before the response, programmers can use this method for storing the generated Ids
- AddProperties() used for add clear information to the response
There is another method in the CredentialsVerifier interface that is involved during the refresh token process. In this case the methods are called in this order:
- ValidateTokenId() called first for TokenId verification, the method receives the TokenId related to the token associated to the refresh token
- AddClaims() used for add information to the token that will be encrypted
- StoreTokenId() called after the token regeneration but before the response, programmers can use this method for storing the generated Ids
- AddProperties() used for add clear information to the response
This snippet shows how to create an authorization server
package main
import (
"time"
"github.com/geekypanda/oauth2server"
"github.com/kataras/iris"
)
func main() {
s := oauth2server.NewOAuthBearerServer(
"mySecretKey-10101",
time.Second*120,
&TestUserVerifier{},
nil)
iris.Post("/token", s.UserCredentials)
iris.Post("/auth", s.ClientCredentials)
iris.Listen(":9090")
}
This snippet shows how to use the middleware
authorized := iris.Party("/authorized")
// use the Bearer Athentication middleware
authorized.Use(oauth2server.Authorize("mySecretKey-10101", nil))
authorized.Get("/customers", GetCustomers)
authorized.Get("/customers/:id/orders", GetOrders)
Note that the authorization server and the authorization middleware are both using the same token formatter and the same secret key for encryption/decryption.