Skip to content

Commit

Permalink
Merge tag 'v0.4.29' into k8s-lib-update-v0.4.20
Browse files Browse the repository at this point in the history
  • Loading branch information
kartik-579 committed Jun 8, 2022
2 parents 61a0522 + 9a4123d commit dcd017c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
9 changes: 9 additions & 0 deletions apiToken/ApiTokenSecretStore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package apiTokenAuth

type ApiTokenSecretStore struct {
Secret string
}

func InitApiTokenSecretStore() *ApiTokenSecretStore {
return &ApiTokenSecretStore{}
}
40 changes: 33 additions & 7 deletions middleware/sessionmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@
* Some of the code has been taken from argocd, for them argocd licensing terms apply
*/


package middleware

import (
"context"
"errors"
"fmt"
apiTokenAuth "github.com/devtron-labs/authenticator/apiToken"
"github.com/devtron-labs/authenticator/client"
jwt2 "github.com/devtron-labs/authenticator/jwt"
"github.com/devtron-labs/authenticator/oidc"
jwt "github.com/golang-jwt/jwt/v4"
"github.com/golang-jwt/jwt/v4"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"net"
Expand All @@ -35,15 +36,19 @@ import (

// SessionManager generates and validates JWT tokens for login sessions.
type SessionManager struct {
settings *oidc.Settings
client *http.Client
prov oidc.Provider
settings *oidc.Settings
client *http.Client
prov oidc.Provider
apiTokenSecretStore *apiTokenAuth.ApiTokenSecretStore
}

const (
// SessionManagerClaimsIssuer fills the "iss" field of the token.
SessionManagerClaimsIssuer = "argocd"

// ApiTokenClaimIssuer is the issuer who generated api-token for APIs
ApiTokenClaimIssuer = "apiTokenIssuer"

// invalidLoginError, for security purposes, doesn't say whether the username or password was invalid. This does not mitigate the potential for timing attacks to determine which is which.
invalidLoginError = "Invalid username or password"
blankPasswordError = "Blank passwords are not allowed"
Expand All @@ -59,9 +64,10 @@ var (
)

// NewSessionManager creates a new session manager from Argo CD settings
func NewSessionManager(settings *oidc.Settings, config *client.DexConfig) *SessionManager {
func NewSessionManager(settings *oidc.Settings, config *client.DexConfig, apiTokenSecretStore *apiTokenAuth.ApiTokenSecretStore) *SessionManager {
s := SessionManager{
settings: settings,
settings: settings,
apiTokenSecretStore: apiTokenSecretStore,
}
s.client = &http.Client{
Transport: &http.Transport{
Expand Down Expand Up @@ -165,6 +171,23 @@ func (mgr *SessionManager) Parse(tokenString string) (jwt.Claims, error) {
return token.Claims, nil
}

// ParseApiToken tries to parse the provided string and returns the token claims for api-token user.
func (mgr *SessionManager) ParseApiToken(tokenString string) (jwt.Claims, error) {
var claims jwt.MapClaims

token, err := jwt.ParseWithClaims(tokenString, &claims, func(token *jwt.Token) (interface{}, error) {
return []byte(mgr.apiTokenSecretStore.Secret), nil
})

if err != nil {
return nil, err
}
if !token.Valid {
return nil, errors.New("token is invalid")
}
return token.Claims, nil
}

// VerifyToken verifies if a token is correct. Tokens can be issued either from us or by an IDP.
// We choose how to verify based on the issuer.
func (mgr *SessionManager) VerifyToken(tokenString string) (jwt.Claims, error) {
Expand All @@ -180,6 +203,9 @@ func (mgr *SessionManager) VerifyToken(tokenString string) (jwt.Claims, error) {
case SessionManagerClaimsIssuer:
// Argo CD signed token
return mgr.Parse(tokenString)
case ApiTokenClaimIssuer:
// api-key token
return mgr.ParseApiToken(tokenString)
default:
// IDP signed token
prov, err := mgr.provider()
Expand Down

0 comments on commit dcd017c

Please sign in to comment.