Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 177 lines (172 sloc) 4.681 kb
b3a2c3e1 » leanucci
2011-03-01 Version Bump to 0.0.1
1 #!/bin/bash
2 # FUNCTION: Bash script to get a TA from WSAA
3 # AUTHOR: Gerardo Fisanotti - AFIP/SDG-SI/DIITEC/DEARIN - 15-nov-2010
4 # Dependencies: curl, openssl >= 1.0, xmllint
5 #
6 # Modify following definitions according to your environment:
7 #
8c0eb2ec » leanucci
2011-01-13 Basic functionality is there.
8 # URL=https://wsaahomo.afip.gov.ar/ws/services/LoginCms # WSAA URL
9 # KEY=spec/fixtures/pkey # file containing the private key in PEM format
10 # CRT=spec/fixtures/cert.crt # file containing the X.509 certificate in PEM format
b3a2c3e1 » leanucci
2011-03-01 Version Bump to 0.0.1
11 TAFN="TA.xml" # file name of the output file
12 # modify next line if you need a proxy to get to the Internet or comment it out
13 # if you don't need a proxy
14 # export https_proxy="http://10.20.152.112:80"
15 #
16 # No further modifications should be needed below this line
17 #==============================================================================
6cfa30f2 » gagoar
2012-04-15 openssl location change depending on the S.O setup
18
b3a2c3e1 » leanucci
2011-03-01 Version Bump to 0.0.1
19 function MakeTRA()
20 #
21 # Generate the XML containing the Access Ticket Request (TRA)
22 #
23 {
24 # FROM=$(date -j -f "%a %b %d %T %Z %Y" "`date -v0H -v0M -v0S`" "+%s")
25 # TO=$(date -j -f "%a %b %d %T %Z %Y" "`date -v23H -v59M -v59S`" "+%s")
26 FROM=$(date "+%Y-%m-%dT00:00:00-03:00")
27 TO=$(date "+%Y-%m-%dT23:59:59-03:00")
28 ID=$(date "+%s")
29 TRA=$(cat <<EOF
30 <?xml version="1.0" encoding="UTF-8"?>
31 <loginTicketRequest version="1.0">
32 <header>
33 <uniqueId>$ID</uniqueId>
34 <generationTime>$FROM</generationTime>
35 <expirationTime>$TO</expirationTime>
36 </header>
37 <service>wsfe</service>
38 </loginTicketRequest>
39 EOF
40 )
41 }
42 #------------------------------------------------------------------------------
43 function MakeCMS()
44 #
45 # Generate de CMS container (TRA + sign + certificate)
46 #
47 {
6cfa30f2 » gagoar
2012-04-15 openssl location change depending on the S.O setup
48 OPENSSL=$(which openssl)
b3a2c3e1 » leanucci
2011-03-01 Version Bump to 0.0.1
49 CMS=$(
50 echo "$TRA" |
6cfa30f2 » gagoar
2012-04-15 openssl location change depending on the S.O setup
51 $OPENSSL cms -sign -in /dev/stdin -signer $CRT -inkey $KEY -nodetach \
b3a2c3e1 » leanucci
2011-03-01 Version Bump to 0.0.1
52 -outform der |
6cfa30f2 » gagoar
2012-04-15 openssl location change depending on the S.O setup
53 $OPENSSL base64 -e
b3a2c3e1 » leanucci
2011-03-01 Version Bump to 0.0.1
54 )
55 }
56 #------------------------------------------------------------------------------
57 function MakeSOAPrequest()
58 #
59 # Generate the SOAP request XML
60 #
61 {
62 REQUEST=$(cat <<EOF
63 <?xml version="1.0" encoding="UTF-8"?>
64 <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://wsaa.view.sua.dvadac.desein.afip.gov">
65 <SOAP-ENV:Body>
66 <ns1:loginCms>
67 <ns1:in0>
68 $CMS
69 </ns1:in0>
70 </ns1:loginCms>
71 </SOAP-ENV:Body>
72 </SOAP-ENV:Envelope>
73 EOF
74 )
75 }
76 #------------------------------------------------------------------------------
77 function CallWSAA()
78 #
79 # Invoke WSAA sending SOAP request XML to LoginCMS method
80 #
81 {
82 RESPONSE=$(
83 echo "$REQUEST" |
8c0eb2ec » leanucci
2011-01-13 Basic functionality is there.
84 curl -k -H 'Content-Type: application/soap+xml; action=""' -d @- $URL
b3a2c3e1 » leanucci
2011-03-01 Version Bump to 0.0.1
85 )
86 echo "$REQUEST"
87 }
88 #------------------------------------------------------------------------------
89 function ParseTA()
90 #
91 # Try to parse the results obtained from WSAA
92 #
93 {
94 TOKEN=$(
95 echo "$RESPONSE" |
96 grep token |
97 sed -e 's/&lt;token&gt;//' |
98 sed -e 's/&lt;\/token&gt;//' |
99 sed -e 's/ //g'
100 )
101 SIGN=$(
102 echo "$RESPONSE" |
103 grep sign |
104 sed -e 's/&lt;sign&gt;//' |
105 sed -e 's/&lt;\/sign&gt;//' |
106 sed -e 's/ //g'
107 )
108 # If we did not get TOKEN, then it was a SOAP Fault, show the error message
109 # and exit
110 #
111 if [ "$TOKEN" == "" ]
112 then
113 echo "ERROR: "
114 echo "$(echo "$RESPONSE" | xmllint --format - | grep faultstring)"
115 exit 1
116 fi
117 }
118 #------------------------------------------------------------------------------
119 function WriteTA()
120 #
121 # Write the token and sign to the output file
122 #
123 {
124 cat <<EOF > $TAFN
125 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
126 <loginTicketResponse version="1">
127 <credentials>
128 <token>$TOKEN</token>
129 <sign>$SIGN</sign>
130 </credentials>
131 </loginTicketResponse>
132 EOF
133 }
134
135 function WriteYAML()
136 {
5c9f467e »
2011-12-22 Add CUIT into file's name so more than one can be used per computer.
137 cat <<EOF > /tmp/bravo_${CUIT}_$(date +"%d_%m_%Y").yml
b3a2c3e1 » leanucci
2011-03-01 Version Bump to 0.0.1
138 token: '$TOKEN'
139 sign: '$SIGN'
140 EOF
141 }
6cfa30f2 » gagoar
2012-04-15 openssl location change depending on the S.O setup
142
143
b3a2c3e1 » leanucci
2011-03-01 Version Bump to 0.0.1
144 #------------------------------------------------------------------------------
145 #
146 # MAIN program
147 #
148 # If we were invoked with a service name in arg #1, use it
149 #[ $# -eq 1 ] && SERVICE=$1
150 # otherwise, ask for it
151 #[ $# -eq 0 ] && read -p "Service name: " SERVICE
8c0eb2ec » leanucci
2011-01-13 Basic functionality is there.
152
153 # Parse commandline arguments
5c9f467e »
2011-12-22 Add CUIT into file's name so more than one can be used per computer.
154 while getopts 'k:u:c:i:' OPTION
8c0eb2ec » leanucci
2011-01-13 Basic functionality is there.
155 do
156 case $OPTION in
157 c) CRT=$OPTARG
158 ;;
159 k) KEY=$OPTARG
160 ;;
161 u) URL=$OPTARG
162 ;;
5c9f467e »
2011-12-22 Add CUIT into file's name so more than one can be used per computer.
163 i) CUIT=$OPTARG
164 ;;
8c0eb2ec » leanucci
2011-01-13 Basic functionality is there.
165 esac
166 done
167 shift $(($OPTIND - 1))
b3a2c3e1 » leanucci
2011-03-01 Version Bump to 0.0.1
168 MakeTRA # Generate TRA
169 MakeCMS # Generate CMS (TRA + signature + certificate)
170 MakeSOAPrequest # Generate the SOAP request XML
171 CallWSAA # Invoke WSAA sending SOAP request
172 ParseTA # Parse the WSAA SOAP response, extract Token and Sign
173 # WriteTA # Write an abbreviated TA.xml with Token and Sign only
174 WriteYAML
175 echo "Access Ticket acquired, written to: $TAFN" # Inform success and exit
176 echo $REQUEST
177 echo $TRA
Something went wrong with that request. Please try again.