syntax = "proto3";
package mairu;

import "google/protobuf/timestamp.proto";

message Session {
  uint32 id = 1;
  string server_id = 2;
  string server_url = 3;
  google.protobuf.Timestamp expires_at = 4;
  bool refreshable = 5;
}

message Credentials {
  int64 version = 1;
  string access_key_id = 2;
  string secret_access_key = 3;
  string session_token = 4;
  google.protobuf.Timestamp expiration = 5;
}

service Agent {
  rpc PingAgent(PingAgentRequest) returns (PingAgentResponse);
  rpc GetServer(GetServerRequest) returns (GetServerResponse);
  rpc AssumeRole(AssumeRoleRequest) returns (AssumeRoleResponse);
  rpc ListSessions(ListSessionsRequest) returns (ListSessionsResponse);
  // rpc RemoveSession(RemoveSessionRequest) returns (RemoveSessionResponse)
  rpc RefreshSession(RefreshSessionRequest) returns (RefreshSessionResponse);

  rpc InitiateOauthCode(InitiateOAuthCodeRequest) returns (InitiateOAuthCodeResponse);
  rpc CompleteOauthCode(CompleteOAuthCodeRequest) returns (CompleteOAuthCodeResponse);

  rpc InitiateOauthDeviceCode(InitiateOAuthDeviceCodeRequest) returns (InitiateOAuthDeviceCodeResponse);
  rpc CompleteOauthDeviceCode(CompleteOAuthDeviceCodeRequest) returns (CompleteOAuthDeviceCodeResponse);

  rpc RefreshAwsSsoClientRegistration(RefreshAwsSsoClientRegistrationRequest) returns (RefreshAwsSsoClientRegistrationResponse);

  rpc ListRoles(ListRolesRequest) returns (ListRolesResponse);

  rpc ShutdownAgent(ShutdownAgentRequest) returns (ShutdownAgentResponse);
}

message PingAgentRequest {
}
message PingAgentResponse {
  string version = 1;
}

message GetServerRequest {
  string query = 1;
  bool no_cache = 2;
  bool check_session = 3;
}
message GetServerResponse {
  string json = 1;
  bool cached = 2;
  Session session = 3;
}

message AssumeRole {
  string role_arn = 1;
  optional int32 duration_seconds = 2;
  optional string external_id = 3;
  optional string role_session_name = 4;
}

message Rolespec {
  string role = 1;
  AssumeRole assume_role = 2;
}

message AssumeRoleRequest {
  string server_id = 1;
  oneof query {
    string role = 2;
    Rolespec rolespec = 4;
  }
  bool cached = 3;
}
message AssumeRoleResponse {
  Credentials credentials = 1;
}

message ListSessionsRequest {
}
message ListSessionsResponse {
  repeated Session sessions = 1;
}

message RemoveSessionRequest {
  string query = 1;
}
message RemoveSessionResponse {
}
message RefreshSessionRequest {
  string query = 1;
}
message RefreshSessionResponse {
  Session session = 1;
}

message InitiateOAuthCodeRequest {
  string server_id = 1;
  string redirect_url = 2;
}
message InitiateOAuthCodeResponse {
  string handle = 1;
  string authorize_url = 2;
}

message CompleteOAuthCodeRequest {
  string handle = 1;
  string code = 2;
  string state = 3;
}
message CompleteOAuthCodeResponse {
}

message InitiateOAuthDeviceCodeRequest {
  string server_id = 1;
}
message InitiateOAuthDeviceCodeResponse {
  string handle = 1;
  string user_code = 2;
  string verification_uri = 3;
  string verification_uri_complete = 4;
  google.protobuf.Timestamp expires_at = 5;
  int32 interval = 6;
}

message CompleteOAuthDeviceCodeRequest {
  string handle = 1;
}
message CompleteOAuthDeviceCodeResponse {
}

message RefreshAwsSsoClientRegistrationRequest {
  string server_id = 1;
  bool force = 2;
}
message RefreshAwsSsoClientRegistrationResponse {
}

message ListRolesRequest {
  string server_id = 1;
}
message ListRolesResponse {
  message Item {
    message Role {
      string name = 1;
      string description = 2;
    }
    string server_id = 1;
    string server_url = 2;
    bool logged_in = 3;
    repeated Role roles = 4;
  }
  repeated Item servers = 1;
}

message ShutdownAgentRequest {
}
message ShutdownAgentResponse {
}

//----- The following is used for mairu-exec subprocess IPC

message ExecEnvVar {
  string name = 1;
  string value = 2;
}
message ExecEnvironment {
  repeated ExecEnvVar set_vars = 1;
  repeated string remove_vars = 2;
}
// NOTE: ExecEnvironmentAction (helper enum) is implemented directly on proto.rs

message ExecIpcInformExecutorRequest {
  uint32 version = 1;
  message Ready {
    ExecEnvironment environment = 1;
  }
  message Failure {
    string error_message = 1;
  }
  oneof result {
    Ready ready = 2;
    Failure failure = 3;
  }
}
