Skip to content

Commit

Permalink
feat: Added Sms, Email, Refresh token (#45)
Browse files Browse the repository at this point in the history
* feat: Sms, Email, Refresh token

Added sms, email and refresh token implementation

* Added email and sms interfaces

* Fixed format
  • Loading branch information
dshilovprpl authored Jan 30, 2024
1 parent 785f2ab commit 6866617
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/email.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import { AxiosResponse } from 'axios'
import { Config } from './config'
import Request from './request'

export interface Email {
title: string
content: string
sender: string
receivers: string[]
}

export class EmailSDK {
private config: Config
private readonly request: Request

constructor(config: Config, request: Request) {
this.config = config
this.request = request
}

public async sendEmail(email: Email) {
if (!this.request) {
throw new Error('request init failed')
}

return (await this.request.post(
'/send-email',
email,
)) as unknown as Promise<AxiosResponse<Record<string, unknown>>>
}
}
18 changes: 18 additions & 0 deletions src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import { Subscription, SubscriptionSDK } from './subscription'
import { Token, TokenSDK } from './token'
import { Webhook, WebhookSDK } from './webhook'
import { Product, ProductSDK } from './product'
import { Email, EmailSDK } from './email'
import { Sms, SmsSDK } from './sms'

export class SDK {
private readonly config: Config
Expand All @@ -60,6 +62,8 @@ export class SDK {
private tokenSDK: TokenSDK
private webhookSDK: WebhookSDK
private productSDK: ProductSDK
private emailSDK: EmailSDK
private smsSDK: SmsSDK

constructor(config: Config) {
this.config = config
Expand Down Expand Up @@ -95,12 +99,18 @@ export class SDK {
this.tokenSDK = new TokenSDK(this.config, this.request)
this.webhookSDK = new WebhookSDK(this.config, this.request)
this.productSDK = new ProductSDK(this.config, this.request)
this.emailSDK = new EmailSDK(this.config, this.request)
this.smsSDK = new SmsSDK(this.config, this.request)
}

public async getAuthToken(code: string) {
return await this.userSDK.getAuthToken(code)
}

public async refreshToken(refreshToken: string) {
return await this.userSDK.refreshToken(refreshToken)
}

public parseJwtToken(token: string) {
return this.userSDK.parseJwtToken(token)
}
Expand Down Expand Up @@ -546,4 +556,12 @@ export class SDK {
public async deleteProduct(product: Product) {
return await this.productSDK.deleteProduct(product)
}

public async sendEmail(email: Email) {
return await this.emailSDK.sendEmail(email)
}

public async sendSms(sms: Sms) {
return await this.smsSDK.sendSms(sms)
}
}
42 changes: 42 additions & 0 deletions src/sms.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import { AxiosResponse } from 'axios'
import { Config } from './config'
import Request from './request'

export interface Sms {
content: string
receivers: string[]
}

export class SmsSDK {
private config: Config
private readonly request: Request

constructor(config: Config, request: Request) {
this.config = config
this.request = request
}

public async sendSms(sms: Sms) {
if (!this.request) {
throw new Error('request init failed')
}

return (await this.request.post('/send-sms', sms)) as unknown as Promise<
AxiosResponse<Record<string, unknown>>
>
}
}
20 changes: 20 additions & 0 deletions src/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,26 @@ export class UserSDK {
return { access_token: access_token, refresh_token: refresh_token }
}

public async refreshToken(refreshToken: string) {
if (!this.request) {
throw new Error('request init failed')
}

const {
data: { access_token, refresh_token },
} = (await this.request.post('login/oauth/refresh_token', {
client_id: this.config.clientId,
client_secret: this.config.clientSecret,
grant_type: 'refresh_token',
refresh_token: refreshToken,
})) as unknown as AxiosResponse<{
access_token: string
refresh_token: string
}>

return { access_token: access_token, refresh_token: refresh_token }
}

public parseJwtToken(token: string) {
return jwt.verify(token, this.config.certificate, {
algorithms: ['RS256'],
Expand Down

0 comments on commit 6866617

Please sign in to comment.