Documentation
¶
Index ¶
- Constants
- Variables
- func Base64decode(v string) (string, error)
- func BasicAuth(user, password string) string
- func BuildPKISerial() (*big.Int, error)
- func Decrypt(data []byte, passphrase string) ([]byte, error)
- func EncodePrivateKeyToPEM(privateKey *rsa.PrivateKey) []byte
- func EncodePublicKeyToPEM(publicKey *rsa.PublicKey) []byte
- func Encrypt(data []byte, passphrase string) ([]byte, error)
- func NewClient(ctx context.Context, conf *ClientConfiguration) *http.Client
- type ClientConfiguration
- type Internal
- type JWTKey
- type OAuth2
- type SignClaims
- type Token
- type User
- type UserJWTClaims
Examples ¶
Constants ¶
const ( ScopeOpenID = "openid" ScopeProfile = "profile" ScopeEmail = "email" ScopeGroups = "groups" ScopeInternal = "internal" )
const ServiceAccountPrefix = "@oauth2"
ServiceAccountPrefix email domain for service accounts.
const SignAlgo = "RS256"
SignAlgo const.
Variables ¶
var AllScopes = []string{ScopeOpenID, ScopeProfile, ScopeEmail, ScopeGroups, ScopeInternal}
Functions ¶
func Base64decode ¶
Base64decode decodes base64 input to string.
func BasicAuth ¶
BasicAuth returns a base64 encoded string of the user and password.
Example ¶
package main
import (
"encoding/base64"
"fmt"
"github.com/elisasre/go-common/v2/auth"
)
func main() {
encoded := auth.BasicAuth("username", "password")
out, err := base64.StdEncoding.DecodeString(encoded)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(out))
}
Output: username:password
func BuildPKISerial ¶
BuildPKISerial generates random big.Int.
func Decrypt ¶
Decrypt the encrypted secret with passphrase.
Example ¶
package main
import (
"fmt"
"github.com/elisasre/go-common/v2/auth"
)
func main() {
encrypted, _ := auth.Encrypt([]byte("supersecret"), "testpassword")
data, _ := auth.Decrypt(encrypted, "testpassword")
fmt.Println(string(data))
}
Output: supersecret
func EncodePrivateKeyToPEM ¶
func EncodePrivateKeyToPEM(privateKey *rsa.PrivateKey) []byte
func EncodePublicKeyToPEM ¶
func Encrypt ¶
Encrypt the secret input with passphrase source https://www.thepolyglotdeveloper.com/2018/02/encrypt-decrypt-data-golang-application-crypto-packages/
Example ¶
package main
import (
"fmt"
"github.com/elisasre/go-common/v2/auth"
)
func main() {
encrypted, _ := auth.Encrypt([]byte("supersecret"), "testpassword")
data, _ := auth.Decrypt(encrypted, "testpassword")
fmt.Println(string(data))
}
Output: supersecret
Types ¶
type ClientConfiguration ¶
type ClientConfiguration struct {
OAuth2
}
type Internal ¶
type Internal struct {
Cluster *string `json:"cluster,omitempty"`
ChangeLimit *int `json:"limit,omitempty"`
MFA *bool `json:"mfa"`
EmployeeID string `json:"employeeid,omitempty"`
}
Internal contains struct for internal non standard variables.
type JWTKey ¶
type JWTKey struct {
CreatedAt time.Time `yaml:"created_at" json:"created_at"`
KID string `yaml:"kid" json:"kid"`
PrivateKey *rsa.PrivateKey `yaml:"-" json:"-"`
PublicKey *rsa.PublicKey `yaml:"-" json:"-"`
}
JWTKey is struct for storing auth private keys.
func GenerateNewKeyPair ¶
GenerateNewKeyPair generates new private and public keys.
type SignClaims ¶
type SignClaims struct {
Aud string
Exp int64
Iat int64
Issuer string
Nonce string
Scopes []string
}
SignClaims contains claims that are passed to SignExpires func.
type Token ¶
type Token struct {
User *User
}
Token struct.
func (*Token) SignExpires ¶
func (t *Token) SignExpires(key JWTKey, claim SignClaims) (string, error)
SignExpires makes new jwt token using expiration time and secret.
type User ¶
type User struct {
Groups []string `json:"groups,omitempty"`
Eid string `json:"custom:employeeid,omitempty"`
Department string `json:"custom:department,omitempty"`
JobTitle string `json:"custom:jobtitle,omitempty"`
ImportGroups []string `json:"cognito:groups,omitempty"`
Email *string `json:"email,omitempty"`
EmailVerified *bool `json:"email_verified,omitempty"`
Name *string `json:"name,omitempty"`
Internal *Internal `json:"internal,omitempty"`
}
User contains struct for single user.
func (User) IsServiceAccount ¶
IsServiceAccount returns boolean is the account service account.
type UserJWTClaims ¶
type UserJWTClaims struct {
*User
jwt.RegisteredClaims
Nonce string `json:"nonce,omitempty"`
}
UserJWTClaims contains struct for making and parsing jwt tokens.
func ParseToken ¶
func ParseToken(raw string, keys []JWTKey, options ...jwt.ParserOption) (*UserJWTClaims, error)
ParseToken will validate jwt token and return user with jwt claims.