| 1 | import { ApiProperty, PartialType } from '@nestjs/swagger'; |
| 2 | import { |
| 3 | ArrayMaxSize, |
| 4 | ArrayMinSize, |
| 5 | ArrayUnique, |
| 6 | IsArray, |
| 7 | IsBoolean, |
| 8 | IsOptional, |
| 9 | IsString, |
| 10 | IsUrl, |
| 11 | Matches, |
| 12 | MaxLength, |
| 13 | } from 'class-validator'; |
| 14 | |
| 15 | export const MAX_NAME_LENGTH = |
| 16 | export const MAX_DESCRIPTION_LENGTH = |
| 17 | export const MAX_CALLBACK_URLS = |
| 18 | |
| 19 | /** https off the loopback host, or plain http on it for local development: the shapes the oauth-provider plugin accepts from a native client. */ |
| 20 | const CALLBACK_URL = |
| 21 | /^(https:\/\/(?! |
| 22 | |
| 23 | export class CreateOauthAppDTO |
| 24 | @ApiProperty({ maxLength |
| 25 | @IsString() |
| 26 | @MaxLength(MAX_NAME_LENGTH) |
| 27 | name: string; |
| 28 | |
| 29 | @ApiProperty({ |
| 30 | required: false, |
| 31 | maxLength: MAX_DESCRIPTION_LENGTH, |
| 32 | description: 'Shown to users when they are asked to authorize the app.', |
| 33 | }) |
| 34 | @IsOptional() |
| 35 | @IsString() |
| 36 | @MaxLength(MAX_DESCRIPTION_LENGTH) |
| 37 | description?: string |
| 38 | |
| 39 | @ApiProperty({ example |
| 40 | @IsUrl({ protocols |
| 41 | homepageUrl: string; |
| 42 | |
| 43 | @ApiProperty({ |
| 44 | type: [String], |
| 45 | minItems: 1, |
| 46 | maxItems: MAX_CALLBACK_URLS, |
| 47 | description: |
| 48 | "Where users may return after authorizing. A redirect_uri must share one's scheme, host and port, with a path equal to or under its path; without one, users return to the first.", |
| 49 | example: ['https://bot.example/oauth/callback'], |
| 50 | }) |
| 51 | @IsArray() |
| 52 | @ArrayMinSize(1) |
| 53 | @ArrayMaxSize(MAX_CALLBACK_URLS) |
| 54 | @ArrayUnique() |
| 55 | @IsUrl({ protocols |
| 56 | @Matches(CALLBACK_URL, |
| 57 | each: true, |
| 58 | message: 'Each callback URL must use https, or http on localhost', |
| 59 | }) |
| 60 | callbackUrls: string[] |
| 61 | |
| 62 | @ApiProperty({ |
| 63 | required: false, |
| 64 | default: false, |
| 65 | description: 'Lets the app sign users in with the device flow, as gh does.', |
| 66 | }) |
| 67 | @IsOptional() |
| 68 | @IsBoolean() |
| 69 | deviceFlowEnabled?: boolean |
| 70 | |
| 71 | @ApiProperty({ |
| 72 | required: false, |
| 73 | default: false, |
| 74 | description: |
| 75 | 'Access tokens expire after 8 hours and come with a refresh token that lasts 6 months, like GitHub\'s "Expire user authorization tokens".', |
| 76 | }) |
| 77 | @IsOptional() |
| 78 | @IsBoolean() |
| 79 | expireUserTokens?: boolean |
| 80 | } |
| 81 | |
| 82 | export class UpdateOauthAppDTO |
| 83 | |
| 84 | export class OauthAppDTO |
| 85 | @ApiProperty() |
| 86 | clientId: string; |
| 87 | |
| 88 | @ApiProperty() |
| 89 | name: string; |
| 90 | |
| 91 | @ApiProperty({ type |
| 92 | description: string |
| 93 | |
| 94 | @ApiProperty() |
| 95 | homepageUrl: string; |
| 96 | |
| 97 | @ApiProperty({ type |
| 98 | logoUrl: string |
| 99 | |
| 100 | @ApiProperty({ type |
| 101 | callbackUrls: string[] |
| 102 | |
| 103 | @ApiProperty() |
| 104 | deviceFlowEnabled: boolean; |
| 105 | |
| 106 | @ApiProperty() |
| 107 | expireUserTokens: boolean; |
| 108 | |
| 109 | @ApiProperty({ description |
| 110 | createdAt: string; |
| 111 | } |
| 112 | |
| 113 | export class CreatedOauthAppDTO |
| 114 | @ApiProperty({ |
| 115 | description: 'Shown only now. Store it: Ghost keeps only a hash.', |
| 116 | }) |
| 117 | clientSecret: string; |
| 118 | } |
| 119 | |
| 120 | export class OauthAppSecretDTO |
| 121 | @ApiProperty({ |
| 122 | description: |
| 123 | 'The new secret, shown only now. The previous one stops working at once.', |
| 124 | }) |
| 125 | clientSecret: string; |
| 126 | } |
| 127 | |
| 128 | export class ListOauthAppsResponseDTO |
| 129 | @ApiProperty({ type |
| 130 | apps: OauthAppDTO |
| 131 | } |
| 132 | |
| 133 | export class AuthorizingOauthAppDTO |
| 134 | @ApiProperty() |
| 135 | clientId: string; |
| 136 | |
| 137 | @ApiProperty() |
| 138 | name: string; |
| 139 | |
| 140 | @ApiProperty({ type |
| 141 | description: string |
| 142 | |
| 143 | @ApiProperty() |
| 144 | homepageUrl: string; |
| 145 | |
| 146 | @ApiProperty({ type |
| 147 | logoUrl: string |
| 148 | |
| 149 | @ApiProperty({ |
| 150 | type: String, |
| 151 | nullable: true, |
| 152 | description: |
| 153 | 'Username or organization slug of the owner that registered the app; null for an app built into Ghost.', |
| 154 | }) |
| 155 | owner: string |
| 156 | |
| 157 | @ApiProperty({ |
| 158 | description: |
| 159 | 'Vouched for by the operators of this Ghost instance. Owners cannot set it.', |
| 160 | }) |
| 161 | verified: boolean; |
| 162 | } |
| 163 | |
| 164 | export class AuthorizedOauthAppDTO |
| 165 | @ApiProperty() |
| 166 | clientId: string; |
| 167 | |
| 168 | @ApiProperty() |
| 169 | name: string; |
| 170 | |
| 171 | @ApiProperty({ type |
| 172 | logoUrl: string |
| 173 | |
| 174 | @ApiProperty({ |
| 175 | type: [String], |
| 176 | description: |
| 177 | 'Every scope the app holds across its keys and refresh tokens for this account.', |
| 178 | }) |
| 179 | scopes: string[] |
| 180 | |
| 181 | @ApiProperty({ description |
| 182 | authorizedAt: string; |
| 183 | |
| 184 | @ApiProperty({ |
| 185 | type: String, |
| 186 | nullable: true, |
| 187 | description: 'When one of its keys was last used, ISO 8601.', |
| 188 | }) |
| 189 | lastUsedAt: string |
| 190 | } |
| 191 | |
| 192 | export class ListAuthorizedOauthAppsResponseDTO |
| 193 | @ApiProperty({ type |
| 194 | apps: AuthorizedOauthAppDTO |
| 195 | } |