WWW-Authentik

 view release on metacpan or  search on metacpan

t/lib/FakeAuthentik.pm  view on Meta::CPAN


my $JSON = JSON::MaybeXS->new( utf8 => 1, canonical => 1, allow_nonref => 1 );

# collection => how it behaves. `path` is the list address, `detail` the field
# the detail address uses, `key` the field a duplicate is recognised by,
# `model` the name authentik puts into a 404, `required` the fields a create
# needs, `duplicate` the message a second create answers with.
my %COLLECTION = (
  users => {
    path => '/api/v3/core/users/', detail => 'pk', key => 'username', model => 'User', pk => 'int',
    required => [qw( username name )], duplicate => 'This field must be unique.',
    defaults => { is_active => \1, attributes => {}, groups => [], path => 'users', type => 'internal' }
  },
  groups => {
    path => '/api/v3/core/groups/', detail => 'pk', key => 'name', model => 'Group', pk => 'uuid',
    required => [qw( name )], duplicate => 'Group with this name already exists.',
    defaults => { is_superuser => \0, attributes => {}, users => [], parent => undef }
  },
  tokens => {
    path => '/api/v3/core/tokens/', detail => 'identifier', key => 'identifier', model => 'Token', pk => 'uuid',
    required => [qw( identifier )], duplicate => 'Token with this identifier already exists.',
    defaults => { intent => 'api', description => '', managed => undef }
  },
  applications => {
    path => '/api/v3/core/applications/', detail => 'slug', key => 'slug', model => 'Application', pk => 'uuid',
    required => [qw( name slug )], duplicate => 'Application with this slug already exists.',
    defaults => { provider => undef, meta_description => '', meta_launch_url => '', policy_engine_mode => 'any', open_in_new_tab => \0 }
  },
  providers => {
    path => '/api/v3/providers/oauth2/', detail => 'pk', key => 'name', model => 'OAuth2Provider', pk => 'int',
    required => [qw( name authorization_flow invalidation_flow redirect_uris )],
    duplicate => 'provider with this name already exists.',
    defaults => { client_type => 'confidential', grant_types => [], property_mappings => [],
      sub_mode => 'hashed_user_id', issuer_mode => 'per_provider', access_token_validity => 'minutes=5',
      refresh_token_validity => 'days=30', signing_key => undef, include_claims_in_id_token => \1 }
  },
  mappings => {
    path => '/api/v3/propertymappings/provider/scope/', detail => 'pk', key => 'name', model => 'ScopeMapping', pk => 'uuid',
    required => [qw( name expression scope_name )], duplicate => 'Property Mapping with this name already exists.',
    defaults => { managed => undef, description => '', component => 'ak-property-mapping-provider-scope-form' }
  },
  flows => {
    path => '/api/v3/flows/instances/', detail => 'slug', key => 'slug', model => 'Flow', pk => 'uuid',
    required => [qw( name slug title designation )], duplicate => 'Flow with this slug already exists.',
    defaults => { authentication => 'none', layout => 'stacked', denied_action => 'message_continue',
      policy_engine_mode => 'any', compatibility_mode => \0, stages => [] }
  },
  bindings => {
    path => '/api/v3/flows/bindings/', detail => 'pk', key => undef, model => 'FlowStageBinding', pk => 'uuid',
    required => [qw( target stage order )],
    defaults => { evaluate_on_plan => \0, re_evaluate_policies => \1, policy_engine_mode => 'any', invalid_response_action => 'retry' }
  },
  stages => {
    path => '/api/v3/stages/', detail => 'pk', key => 'name', model => 'Stage', pk => 'uuid',
    required => [qw( name )], duplicate => 'stage with this name already exists.',
    defaults => {}
  },
  certificates => {
    path => '/api/v3/crypto/certificatekeypairs/', detail => 'pk', key => 'name', model => 'CertificateKeyPair', pk => 'uuid',
    required => [qw( name )], duplicate => 'Certificate-Key Pair with this name already exists.',
    defaults => { private_key_available => \1, managed => undef }
  },
  brands => {
    path => '/api/v3/core/brands/', detail => 'brand_uuid', key => 'domain', model => 'Brand', pk => 'uuid',
    required => [qw( domain )], duplicate => 'Brand with this domain already exists.',
    defaults => { default => \0 }
  },
  blueprints => {
    path => '/api/v3/managed/blueprints/', detail => 'pk', key => 'name', model => 'BlueprintInstance', pk => 'uuid',
    required => [qw( name )], duplicate => 'Blueprint Instance with this name already exists.',
    defaults => { enabled => \1, status => 'unknown', managed_models => [], metadata => {}, context => {} }
  }
);

# fields authentik ignores when they are written
my @READ_ONLY = qw( pk num_pk uid uuid component verbose_name verbose_name_plural meta_model_name
  assigned_application_slug assigned_application_name launch_url date_joined password_change_date
  last_updated client_id is_superuser cache_count export_url pbm_uuid policybindingmodel_ptr_id );

sub new {
  my ( $class, %arg ) = @_;
  my $self = $class->SUPER::new;
  my $key  = Crypt::PK::RSA->new;
  $key->generate_key( 256, 65537 );
  %$self = (
    %$self,
    base          => 'http://ak.test',
    token         => 'fake-api-token',
    now           => $arg{now} || sub { time },
    expires_in    => $arg{expires_in} // 300,
    key           => $key,
    kid           => 'key-1',
    seq           => 0,
    requests      => [],
    writes        => 0,
    passwords     => {},
    data          => { map { $_ => {} } keys %COLLECTION },
    oauth         => { access => {}, refresh => {}, codes => {}, devices => {} },
    break_paging  => 0
  );
  $self->_seed unless $arg{empty};
  return $self;
}

sub base         { $_[0]{base} }
sub token        { $_[0]{token} }
sub requests     { $_[0]{requests} }
sub writes       { $_[0]{writes} }
sub reset_writes { $_[0]{writes} = 0; return }
sub passwords    { $_[0]{passwords} }
sub collection   { $_[0]{data}{ $_[1] } }
sub kid          { $_[0]{kid} }
sub public_key   { JSON::MaybeXS->new->decode( $_[0]{key}->export_key_jwk('public') ) }
sub break_pagination { $_[0]{break_paging} = defined $_[1] ? $_[1] : 1; return }

sub rotate_key {
  my ( $self ) = @_;
  my $key = Crypt::PK::RSA->new;
  $key->generate_key( 256, 65537 );
  $self->{key} = $key;
  $self->{kid} = 'rotated-'.++$self->{seq};



( run in 0.516 second using v1.01-cache-2.11-cpan-8379b9b8ec2 )