otplessAuthSdk

package module
v1.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 24, 2024 License: MIT Imports: 14 Imported by: 0

README

Merchant Integration Documentation(Backend GoLang Auth SDK)


A. OTPLessAuth Dependency

install Below dependency in your project's

go get github.com/otpless-tech/otpless-auth-sdk@latest

you can also get latest version of dependency at https://pkg.go.dev/github.com/otpless-tech/otpless-auth-sdk/pkg



B. OTPLessAuth

The OTPLessAuth pkg provides methods to integrate OTPLess authentication into your GoLang backend application. This documentation explains the usage of the class and its methods.

Methods:

1. decodeIdToken

This method help to resolve idToken(JWT token) which is issued by OTPLess which return user detail from that token also this method verify that token is valid, token should not expired and issued by only otpless.com

Method Signature:
DecodeIDToken(idToken, clientID, clientSecret, audience string)
Method Params:
Params Data type Mandatory Constraints Remarks
idToken String true idToken which is JWT token which you get from OTPLess by exchange code API
clientId String true Your OTPLess Client Id
clientSecret String true Your OTPLess Client Secret
audience String false None
Return

Return: Object Name: UserDetailResult

2. VerifyCode

This method help to resolve token which is issued by OTPLess which return user detail from that token also this method verify that token is valid, token should not expired and issued by only otpless.com

Method Signature:
VerifyCode(code, clientID, clientSecret)
Method Params:
Params Data type Mandatory Constraints Remarks
code String true code which you get from OTPLess
clientId String true Your OTPLess Client Id
clientSecret String true Your OTPLess Client Secret
Return

Return: Object Name: UserDetailResult

3. VerifyAuthToken

This method help to resolve token which is issued by OTPLess which return user detail from that token also this method verify that token is valid, token should not expired and issued by only otpless.com

Method Signature:
VerifyAuthToken(token, clientID, clientSecret string)
Method Params:
Params Data type Mandatory Constraints Remarks
token String true token which you get from OTPLess
clientId String true Your OTPLess Client Id
clientSecret String true Your OTPLess Client Secret
Return

Return: Object Name: UserDetailResult

userDetail := UserDetailResult{
    Success:     true,
    AuthTime:  1697473071,
    Email:       "dev****@gmail.com",
    Name:        "Dhaval From OTP-less",
    PhoneNumber: "+9193*****",
    country_code: "+91",
    national_phone_number: "95******",
}
UserDetail Object Fields:

success (boolean): This will be true in case of method successfully performed operation.
> authTime (Long, required): The time when authentication was completed.
> phoneNumber (String, required): The user's phone number.
> countryCode (String, required): The country code of user's phone number.
> nationalPhoneNumber (String, required): The user's phone number without country code.
> email (String, required): The user's email address.
> name (String, required): The user's full name.


The Authorization Endpoint initiates the authentication process by sending a magic link to the user's WhatsApp or email, based on the provided contact information. This link is used to verify the identity of the user. Upon the user's action on this link, they are redirected to the specified URI with an authorization code included in the redirection.

Method Signature:
GenerateMagicLink(mobileNumber, email, clientID, clientSecret, redirectUri, channel string) (*MagicLinkResponse, error)
Method Params:
Params Data type Mandatory Constraints Remarks
channel String false if no channel given WHATSAPP is chosen as default WHATSAPP/SMS
mobile_number String false At least one required The user's mobile number for authentication in the format: country code + number (e.g., 91XXXXXXXXXX)
email String false At least one required The user's email address for authentication.
redirect_uri String true The URL to which the user will be redirected after authentication. This should be URL-encoded
clientId String true Your OTPLess Client Id
clientSecret String true Your OTPLess Client Secret
Return

Return: Object Name: RquestIds

&{RequestIds:[{Type:MOBILE Value:c36b678aef104691b15f93910acfee48 DestinationUri:https://wa.me/91971***XX**} {Type:EMAIL Value:39250d56e8da4f4cb86224929bf76a2d}]}
Error case:

success (boolean): This will be false. The method is failed to perform.
errorMessage (String): The message contains error information.

C. OTP class

These methods enable you to send, resend and verify OTP.

Methods:

1. Send OTP

Method Signature:
SendOTP(req SendOTPRequest, clientID, clientSecret string) (*SendOTPResponse, error)

Method Params:
Params Data type Mandatory Constraints Remarks
phoneNumber String true Mobile Number of your users
email String true Mail Id of your users
orderId String false An Merchant unique id for the request.
expiry Int false OTP expiry in sec
hash String false An Hash will be used to auto read OTP.
clientId String true Your OTPLess Client Id
clientSecret String true Your OTPLess Client Secret
otpLength Integer false 4 or 6 only allowed Allowes you to send OTP in 4/6 digit. default will be 6 digit.
channel String false SMS/WHATSAPP/ALL (if no channel given SMS is chosen as default) Allowes you to send OTP on WhatsApp/SMS/Both. default will be SMS
Return

Return: Object Name: SendOTPResponse

{
  "orderId": "Otp_ED5F709E1C6B41EB8C0595C7968354EB"
}
2. Resend OTP
Method Signature:
    ResendOTP(orderID, clientID, clientSecret string) (*ResendOTPResponse, error)
Method Params:
Params Data type Mandatory Constraints Remarks
orderId String true An Merchant unique id for the request.
clientId String true Your OTPLess Client Id
clientSecret String true Your OTPLess Client Secret
Return

Return: Object Name: ResendOTPResponse

{
  "orderId": "DP0000111"
}

3. Verify OTP
Method Signature:
    VerifyOTP(orderID, otp, email, phoneNumber, clientID, clientSecret string) (*VerifyOTPResponse, error)
Method Params:
Params Data type Mandatory Constraints Remarks
email String true An email on which OTP has been sent.
phoneNumber String true An phone number on which OTP has been sent.
orderId String true An Merchant unique id for the request.
otp String true OTP value.
clientId String true Your OTPLess Client Id
clientSecret String true Your OTPLess Client Secret
Return

Return: Object Name: VerifyOTPResponse

  • reason (String): The will be errorMessage in case of OTP doesn't verified
{
  "isOTPVerified": true
}
Example of usage
package main

import (
	"fmt"

	otplessAuthSdk "github.com/otpless-tech/otpless-auth-sdk"
)

func main() {
	clientID := "your_client_id"
	clientSecret := "your_client_secret"
	code := "some_code"

	result, err := otplessAuthSdk.VerifyCode(clientID, clientSecret, code)

	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	fmt.Println("Result:", result)
}

This method allows you to decode and verify OTPLess ID tokens and retrieve user information for integration into your backend GoLang application.

Documentation

Index

Constants

View Source
const (
	OTPLESS_KEY_API     = "https://otpless.com/.well-known/openid-configuration"
	HTTP_TIMEOUT        = 30 * time.Second
	OIDC_AUTH_TOKEN_API = "https://oidc.otpless.app/auth/userInfo"
	MAGIC_LINK_URL      = "https://auth.otpless.app/auth/v1/authorize"
	OTP_BASE_URL        = "https://user-auth.otpless.app/auth/otp"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type MagicLinkResponse

type MagicLinkResponse struct {
	RequestIds []struct {
		Type           string `json:"type"`
		Value          string `json:"value"`
		DestinationUri string `json:"destinationUri,omitempty"`
	} `json:"requestIds"`
}
func GenerateMagicLink(mobileNumber, email, clientID, clientSecret, redirectUri, channel string) (*MagicLinkResponse, error)

type ResendOTPRequest added in v1.0.10

type ResendOTPRequest struct {
	OrderID string `json:"orderId"`
}

type ResendOTPResponse added in v1.0.10

type ResendOTPResponse struct {
	OrderID string `json:"orderId"`
}

func ResendOTP added in v1.0.10

func ResendOTP(orderID, clientID, clientSecret string) (*ResendOTPResponse, error)

type SendOTPRequest added in v1.0.10

type SendOTPRequest struct {
	PhoneNumber string `json:"phoneNumber,omitempty"`
	Email       string `json:"email,omitempty"`
	Channel     string `json:"channel,omitempty"`
	Hash        string `json:"hash,omitempty"`
	OrderId     string `json:"orderId,omitempty"`
	Expiry      int    `json:"expiry,omitempty"`
	OtpLength   int    `json:"otpLength,omitempty"`
	TemplateId  string `json:"templateId,omitempty"`
}

type SendOTPResponse added in v1.0.10

type SendOTPResponse struct {
	OrderID string `json:"orderId"`
}

func SendOTP added in v1.0.10

func SendOTP(req SendOTPRequest, clientID, clientSecret string) (*SendOTPResponse, error)

type UserDetailResult

type UserDetailResult struct {
	Success             bool   `json:"success"`
	AuthTime            int64  `json:"auth_time"`
	Email               string `json:"email"`
	Name                string `json:"name"`
	PhoneNumber         string `json:"phone_number"`
	CountryCode         string `json:"country_code"`
	NationalPhoneNumber string `json:"national_phone_number"`
}

func DecodeIDToken

func DecodeIDToken(idToken, clientID, clientSecret, audience string) (*UserDetailResult, error)

func VerifyAuthToken

func VerifyAuthToken(token, clientID, clientSecret string) (*UserDetailResult, error)

func VerifyCode

func VerifyCode(code, clientID, clientSecret string) (*UserDetailResult, error)

type VerifyOTPRequest added in v1.0.10

type VerifyOTPRequest struct {
	OrderID     string `json:"orderId"`
	OTP         string `json:"otp"`
	Email       string `json:"email"`
	PhoneNumber string `json:"phoneNumber"`
}

type VerifyOTPResponse added in v1.0.10

type VerifyOTPResponse struct {
	IsOTPVerified bool   `json:"isOTPVerified"`
	Reason        string `json:"reason,omitempty"`
}

func VerifyOTP added in v1.0.10

func VerifyOTP(orderID, otp, email, phoneNumber, clientID, clientSecret string) (*VerifyOTPResponse, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL