-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d43e3e2
commit d090078
Showing
1 changed file
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"log" | ||
"strings" | ||
"time" | ||
|
||
"github.com/golang-jwt/jwt/v4" | ||
atlas_claims "github.com/infobloxopen/atlas-claims" | ||
) | ||
|
||
const ( | ||
s2s_audience = "aud" | ||
) | ||
|
||
var ( | ||
// default claims | ||
cfgUserId = "" | ||
cfgIdentityUserId = "" | ||
cfgAccountId = "" | ||
cfgIdentityAccountId = "" | ||
cfgService = atlas_claims.DefaultService | ||
strGroups = "" | ||
cfgSubjectId = "" | ||
cfgSubjectType = atlas_claims.DefaultSubjectType | ||
cfgSubjectAuthId = "" | ||
cfgSubjectAuthType = "" | ||
cfgStandardClaimsAudience = atlas_claims.DefaultAudience | ||
cfgStandardClaimsExpires = time.Duration(time.Hour * 24 * 365).String() | ||
cfgStandardClaimsIssuer = atlas_claims.DefaultIssuer | ||
cfgHmacKey = "" | ||
) | ||
|
||
func main() { | ||
// define flag overrides | ||
flag.StringVar(&cfgUserId, "user_id", cfgUserId, "user id") | ||
flag.StringVar(&cfgIdentityUserId, "identity_user_id", cfgIdentityUserId, "identity user id") | ||
flag.StringVar(&cfgAccountId, "account_id", cfgAccountId, "account id (int)") | ||
flag.StringVar(&cfgIdentityAccountId, "identity_account_id", cfgIdentityAccountId, "identity account id (guid)") | ||
flag.StringVar(&cfgService, "service", cfgService, "service") | ||
flag.StringVar(&strGroups, "groups", strGroups, "groups (comma separated list)") | ||
flag.StringVar(&cfgSubjectId, "subject_id", cfgSubjectId, "subject id") | ||
flag.StringVar(&cfgSubjectType, "subject_type", cfgSubjectType, "subject type") | ||
flag.StringVar(&cfgSubjectAuthId, "subject_auth_id", cfgSubjectAuthId, "subject authentication id") | ||
flag.StringVar(&cfgSubjectAuthType, "subject_auth_type", cfgSubjectAuthType, "subject authentication type") | ||
flag.StringVar(&cfgStandardClaimsAudience, "audience", cfgStandardClaimsAudience, "audience") | ||
flag.StringVar(&cfgStandardClaimsExpires, "expires", cfgStandardClaimsExpires, "expires duration, i.e. 48h") | ||
flag.StringVar(&cfgStandardClaimsIssuer, "issuer", cfgStandardClaimsIssuer, "issuer") | ||
flag.StringVar(&cfgHmacKey, "hmac_key", cfgHmacKey, "default hmac key") | ||
flag.Parse() | ||
|
||
// define claims from cli | ||
cfgGroups := strings.Split(strGroups, ",") | ||
claims := &atlas_claims.Claims{ | ||
UserId: cfgUserId, | ||
IdentityUserId: cfgIdentityUserId, | ||
AccountId: cfgAccountId, | ||
IdentityAccountId: cfgIdentityAccountId, | ||
Service: cfgService, | ||
Groups: cfgGroups, | ||
Subject: atlas_claims.Subject{ | ||
Id: cfgSubjectId, | ||
SubjectType: cfgSubjectType, | ||
AuthenticationId: cfgSubjectAuthId, | ||
AuthenticationType: cfgSubjectType, | ||
}, | ||
StandardClaims: jwt.StandardClaims{ | ||
Audience: cfgStandardClaimsAudience, | ||
Issuer: cfgStandardClaimsIssuer, | ||
}, | ||
} | ||
|
||
// parse expires | ||
dur, err := time.ParseDuration(cfgStandardClaimsExpires) | ||
if err != nil { | ||
log.Fatalf("failed to parse expires duration: %s", err) | ||
} | ||
|
||
// build jwt | ||
tokenString, err := atlas_claims.BuildJwt(claims, cfgHmacKey, dur) | ||
if err != nil { | ||
log.Fatalf("failed to build jwt due to error: %s", err) | ||
} | ||
|
||
// print jwt | ||
fmt.Printf("%v\n", tokenString) | ||
} |