syntax = "proto3";

package svc.auth.v1;

import "google/protobuf/duration.proto";
import "google/protobuf/timestamp.proto";
import "types/v1/environment.proto";
import "types/v1/meta.proto";
import "types/v1/user_token.proto";
import "types/v1/version.proto";

option go_package = "svc/auth/v1;authv1";

service AuthService {
  rpc GetAuthURL(GetAuthURLRequest) returns (GetAuthURLResponse) {}
  rpc BeginDeviceAuth(BeginDeviceAuthRequest) returns (BeginDeviceAuthResponse) {}
  rpc CompleteDeviceAuth(CompleteDeviceAuthRequest) returns (CompleteDeviceAuthResponse) {}
}

message GetAuthURLRequest {
  // optional: if an org is specified
  oneof organization {
    int64 by_id = 100;
    string by_name = 101;
  }
  string return_to_url = 2;
  LocalhostViaBrowser localhost = 3;
}

message LocalhostViaBrowser {
  string architecture = 1;
  string operating_system = 2;
  types.v1.Version using_version = 3;
}

message GetAuthURLResponse {
  string auth_url = 1;
}

message BeginDeviceAuthRequest {
  string organization = 1;
  string return_to_url = 2;
}

message BeginDeviceAuthResponse {
  string url = 1;
  // device_code must be used to retrieve an authenticated user token with `CompleteDeviceAuth`
  string device_code = 2;
  string user_code = 3;
  google.protobuf.Timestamp expires_at = 4;
  google.protobuf.Duration poll_interval = 5;
}

message CompleteDeviceAuthRequest {
  // device_code is returned when calling `BeginDeviceAuth` and allows an auth flow
  // to retrieve the user token that is generated when a user is authenticated
  string device_code = 1;
  string user_code = 2;
  string architecture = 5;
  string operating_system = 6;
}

message CompleteDeviceAuthResponse {
  types.v1.UserToken token = 1;
}
